sha1.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /**
  2. * @file sha1.c SHA-1 in C
  3. */
  4. /*
  5. By Steve Reid <sreid@sea-to-sky.net>
  6. 100% Public Domain
  7. -----------------
  8. Modified 7/98
  9. By James H. Brown <jbrown@burgoyne.com>
  10. Still 100% Public Domain
  11. Corrected a problem which generated improper hash values on 16 bit machines
  12. Routine SHA1Update changed from
  13. void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned int
  14. len)
  15. to
  16. void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned
  17. long len)
  18. The 'len' parameter was declared an int which works fine on 32 bit machines.
  19. However, on 16 bit machines an int is too small for the shifts being done
  20. against
  21. it. This caused the hash function to generate incorrect values if len was
  22. greater than 8191 (8K - 1) due to the 'len << 3' on line 3 of SHA1Update().
  23. Since the file IO in main() reads 16K at a time, any file 8K or larger would
  24. be guaranteed to generate the wrong hash (e.g. Test Vector #3, a million
  25. "a"s).
  26. I also changed the declaration of variables i & j in SHA1Update to
  27. unsigned long from unsigned int for the same reason.
  28. These changes should make no difference to any 32 bit implementations since
  29. an
  30. int and a long are the same size in those environments.
  31. --
  32. I also corrected a few compiler warnings generated by Borland C.
  33. 1. Added #include <process.h> for exit() prototype
  34. 2. Removed unused variable 'j' in SHA1Final
  35. 3. Changed exit(0) to return(0) at end of main.
  36. ALL changes I made can be located by searching for comments containing 'JHB'
  37. -----------------
  38. Modified 8/98
  39. By Steve Reid <sreid@sea-to-sky.net>
  40. Still 100% public domain
  41. 1- Removed #include <process.h> and used return() instead of exit()
  42. 2- Fixed overwriting of finalcount in SHA1Final() (discovered by Chris Hall)
  43. 3- Changed email address from steve@edmweb.com to sreid@sea-to-sky.net
  44. -----------------
  45. Modified 4/01
  46. By Saul Kravitz <Saul.Kravitz@celera.com>
  47. Still 100% PD
  48. Modified to run on Compaq Alpha hardware.
  49. -----------------
  50. Modified 07/2002
  51. By Ralph Giles <giles@artofcode.com>
  52. Still 100% public domain
  53. modified for use with stdint types, autoconf
  54. code cleanup, removed attribution comments
  55. switched SHA1Final() argument order for consistency
  56. use SHA1_ prefix for public api
  57. move public api to sha1.h
  58. */
  59. /*
  60. Test Vectors (from FIPS PUB 180-1)
  61. "abc"
  62. A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
  63. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
  64. 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
  65. A million repetitions of "a"
  66. 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
  67. */
  68. #define SHA1HANDSOFF (1)
  69. #include <stdint.h>
  70. #ifdef HAVE_CONFIG_H
  71. #include "config.h"
  72. #endif
  73. #include <stdio.h>
  74. #include <string.h>
  75. #include "sha.h"
  76. void SHA1_Transform(uint32_t state[5], const uint8_t buffer[64]);
  77. #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
  78. #if defined (BYTE_ORDER) && defined(BIG_ENDIAN) && (BYTE_ORDER == BIG_ENDIAN)
  79. #define WORDS_BIGENDIAN 1
  80. #endif
  81. #ifdef _BIG_ENDIAN
  82. #define WORDS_BIGENDIAN 1
  83. #endif
  84. /* blk0() and blk() perform the initial expand. */
  85. /* I got the idea of expanding during the round function from SSLeay */
  86. /* FIXME: can we do this in an endian-proof way? */
  87. #ifdef WORDS_BIGENDIAN
  88. #define blk0(i) block->l[i]
  89. #else
  90. #define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xff00ff00) \
  91. |(rol(block->l[i],8)&0x00ff00ff))
  92. #endif
  93. #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
  94. ^block->l[(i+2)&15]^block->l[i&15],1))
  95. /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
  96. #define R0(v, w, x, y, z, i) \
  97. z+=((w&(x^y))^y)+blk0(i)+0x5a827999+rol(v,5);w=rol(w,30);
  98. #define R1(v, w, x, y, z, i) \
  99. z+=((w&(x^y))^y)+blk(i)+0x5a827999+rol(v,5);w=rol(w,30);
  100. #define R2(v, w, x, y, z, i) \
  101. z+=(w^x^y)+blk(i)+0x6ed9eba1+rol(v,5);w=rol(w,30);
  102. #define R3(v, w, x, y, z, i) \
  103. z+=(((w|x)&y)|(w&x))+blk(i)+0x8f1bbcdc+rol(v,5);w=rol(w,30);
  104. #define R4(v, w, x, y, z, i) \
  105. z+=(w^x^y)+blk(i)+0xca62c1d6+rol(v,5);w=rol(w,30);
  106. /* Hash a single 512-bit block. This is the core of the algorithm. */
  107. void SHA1_Transform(uint32_t state[5], const uint8_t buffer[64])
  108. {
  109. uint32_t a, b, c, d, e;
  110. typedef union
  111. {
  112. uint8_t c[64];
  113. uint32_t l[16];
  114. } CHAR64LONG16;
  115. CHAR64LONG16 *block;
  116. #ifdef SHA1HANDSOFF
  117. CHAR64LONG16 workspace;
  118. block = &workspace;
  119. memcpy(block, buffer, 64);
  120. #else
  121. block = (CHAR64LONG16 *)buffer;
  122. #endif
  123. /* Copy context->state[] to working vars */
  124. a = state[0];
  125. b = state[1];
  126. c = state[2];
  127. d = state[3];
  128. e = state[4];
  129. /* 4 rounds of 20 operations each. Loop unrolled. */
  130. R0(a, b, c, d, e, 0);
  131. R0(e, a, b, c, d, 1);
  132. R0(d, e, a, b, c, 2);
  133. R0(c, d, e, a, b, 3);
  134. R0(b, c, d, e, a, 4);
  135. R0(a, b, c, d, e, 5);
  136. R0(e, a, b, c, d, 6);
  137. R0(d, e, a, b, c, 7);
  138. R0(c, d, e, a, b, 8);
  139. R0(b, c, d, e, a, 9);
  140. R0(a, b, c, d, e, 10);
  141. R0(e, a, b, c, d, 11);
  142. R0(d, e, a, b, c, 12);
  143. R0(c, d, e, a, b, 13);
  144. R0(b, c, d, e, a, 14);
  145. R0(a, b, c, d, e, 15);
  146. R1(e, a, b, c, d, 16);
  147. R1(d, e, a, b, c, 17);
  148. R1(c, d, e, a, b, 18);
  149. R1(b, c, d, e, a, 19);
  150. R2(a, b, c, d, e, 20);
  151. R2(e, a, b, c, d, 21);
  152. R2(d, e, a, b, c, 22);
  153. R2(c, d, e, a, b, 23);
  154. R2(b, c, d, e, a, 24);
  155. R2(a, b, c, d, e, 25);
  156. R2(e, a, b, c, d, 26);
  157. R2(d, e, a, b, c, 27);
  158. R2(c, d, e, a, b, 28);
  159. R2(b, c, d, e, a, 29);
  160. R2(a, b, c, d, e, 30);
  161. R2(e, a, b, c, d, 31);
  162. R2(d, e, a, b, c, 32);
  163. R2(c, d, e, a, b, 33);
  164. R2(b, c, d, e, a, 34);
  165. R2(a, b, c, d, e, 35);
  166. R2(e, a, b, c, d, 36);
  167. R2(d, e, a, b, c, 37);
  168. R2(c, d, e, a, b, 38);
  169. R2(b, c, d, e, a, 39);
  170. R3(a, b, c, d, e, 40);
  171. R3(e, a, b, c, d, 41);
  172. R3(d, e, a, b, c, 42);
  173. R3(c, d, e, a, b, 43);
  174. R3(b, c, d, e, a, 44);
  175. R3(a, b, c, d, e, 45);
  176. R3(e, a, b, c, d, 46);
  177. R3(d, e, a, b, c, 47);
  178. R3(c, d, e, a, b, 48);
  179. R3(b, c, d, e, a, 49);
  180. R3(a, b, c, d, e, 50);
  181. R3(e, a, b, c, d, 51);
  182. R3(d, e, a, b, c, 52);
  183. R3(c, d, e, a, b, 53);
  184. R3(b, c, d, e, a, 54);
  185. R3(a, b, c, d, e, 55);
  186. R3(e, a, b, c, d, 56);
  187. R3(d, e, a, b, c, 57);
  188. R3(c, d, e, a, b, 58);
  189. R3(b, c, d, e, a, 59);
  190. R4(a, b, c, d, e, 60);
  191. R4(e, a, b, c, d, 61);
  192. R4(d, e, a, b, c, 62);
  193. R4(c, d, e, a, b, 63);
  194. R4(b, c, d, e, a, 64);
  195. R4(a, b, c, d, e, 65);
  196. R4(e, a, b, c, d, 66);
  197. R4(d, e, a, b, c, 67);
  198. R4(c, d, e, a, b, 68);
  199. R4(b, c, d, e, a, 69);
  200. R4(a, b, c, d, e, 70);
  201. R4(e, a, b, c, d, 71);
  202. R4(d, e, a, b, c, 72);
  203. R4(c, d, e, a, b, 73);
  204. R4(b, c, d, e, a, 74);
  205. R4(a, b, c, d, e, 75);
  206. R4(e, a, b, c, d, 76);
  207. R4(d, e, a, b, c, 77);
  208. R4(c, d, e, a, b, 78);
  209. R4(b, c, d, e, a, 79);
  210. /* Add the working vars back into context.state[] */
  211. state[0] += a;
  212. state[1] += b;
  213. state[2] += c;
  214. state[3] += d;
  215. state[4] += e;
  216. /* Wipe variables */
  217. a = b = c = d = e = 0;
  218. }
  219. /**
  220. * Initialize new context
  221. *
  222. * @param context SHA1-Context
  223. */
  224. void SHA1_Init(SHA1_CTX *context)
  225. {
  226. /* SHA1 initialization constants */
  227. context->state[0] = 0x67452301;
  228. context->state[1] = 0xefcdab89;
  229. context->state[2] = 0x98badcfe;
  230. context->state[3] = 0x10325476;
  231. context->state[4] = 0xc3d2e1f0;
  232. context->count[0] = context->count[1] = 0;
  233. }
  234. /**
  235. * Run your data through this
  236. *
  237. * @param context SHA1-Context
  238. * @param p Buffer to run SHA1 on
  239. * @param len Number of bytes
  240. */
  241. void SHA1_Update(SHA1_CTX *context, const void *p, size_t len)
  242. {
  243. const uint8_t *data = p;
  244. size_t i, j;
  245. j = (context->count[0] >> 3) & 63;
  246. if ((context->count[0] += (uint32_t) (len << 3)) < (len << 3))
  247. {
  248. context->count[1]++;
  249. }
  250. context->count[1] += (uint32_t) (len >> 29);
  251. if ((j + len) > 63)
  252. {
  253. memcpy(&context->buffer[j], data, (i = 64 - j));
  254. SHA1_Transform(context->state, context->buffer);
  255. for (; i + 63 < len; i += 64)
  256. {
  257. SHA1_Transform(context->state, data + i);
  258. }
  259. j = 0;
  260. }
  261. else i = 0;
  262. memcpy(&context->buffer[j], &data[i], len - i);
  263. }
  264. /**
  265. * Add padding and return the message digest
  266. *
  267. * @param digest Generated message digest
  268. * @param context SHA1-Context
  269. */
  270. void SHA1_Final(uint8_t digest[SHA1_DIGEST_SIZE], SHA1_CTX *context)
  271. {
  272. uint32_t i;
  273. uint8_t finalcount[8];
  274. for (i = 0; i < 8; i++)
  275. {
  276. finalcount[i] = (uint8_t) ((context->count[(i >= 4 ? 0 : 1)]
  277. >> ((3 - (i & 3)) * 8)) & 255);
  278. }
  279. SHA1_Update(context, (uint8_t *) "\200", 1);
  280. while ((context->count[0] & 504) != 448)
  281. {
  282. SHA1_Update(context, (uint8_t *) "\0", 1);
  283. }
  284. SHA1_Update(context, finalcount, 8); /* Should cause SHA1_Transform */
  285. for (i = 0; i < SHA1_DIGEST_SIZE; i++)
  286. {
  287. digest[i] = (uint8_t)
  288. ((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
  289. }
  290. /* Wipe variables */
  291. i = 0;
  292. memset(context->buffer, 0, 64);
  293. memset(context->state, 0, 20);
  294. memset(context->count, 0, 8);
  295. memset(finalcount, 0, 8); /* SWR */
  296. #ifdef SHA1HANDSOFF /* make SHA1Transform overwrite its own static vars */
  297. SHA1_Transform(context->state, context->buffer);
  298. #endif
  299. }