sha1.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. SHA-1 in C
  3. By Steve Reid <steve@edmweb.com>
  4. 100% Public Domain
  5. Test Vectors (from FIPS PUB 180-1)
  6. "abc"
  7. A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
  8. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
  9. 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
  10. A million repetitions of "a"
  11. 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
  12. */
  13. /* #define LITTLE_ENDIAN * This should be #define'd already, if true. */
  14. /* #define SHA1HANDSOFF * Copies data before messing with it. */
  15. #define SHA1HANDSOFF
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include "sha1.h"
  19. #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
  20. /* blk0() and blk() perform the initial expand. */
  21. /* I got the idea of expanding during the round function from SSLeay */
  22. #if BYTE_ORDER == LITTLE_ENDIAN
  23. #define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
  24. |(rol(block->l[i],8)&0x00FF00FF))
  25. #elif BYTE_ORDER == BIG_ENDIAN
  26. #define blk0(i) block->l[i]
  27. #else
  28. #error "Endianness not defined!"
  29. #endif
  30. #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
  31. ^block->l[(i+2)&15]^block->l[i&15],1))
  32. /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
  33. #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
  34. #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
  35. #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
  36. #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
  37. #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
  38. /* Hash a single 512-bit block. This is the core of the algorithm. */
  39. void HV_SHA1Transform(
  40. uint32_t state[5],
  41. const unsigned char buffer[64]
  42. )
  43. {
  44. uint32_t a, b, c, d, e;
  45. typedef union
  46. {
  47. unsigned char c[64];
  48. uint32_t l[16];
  49. } CHAR64LONG16;
  50. #ifdef SHA1HANDSOFF
  51. CHAR64LONG16 block[1]; /* use array to appear as a pointer */
  52. memcpy(block, buffer, 64);
  53. #else
  54. /* The following had better never be used because it causes the
  55. * pointer-to-const buffer to be cast into a pointer to non-const.
  56. * And the result is written through. I threw a "const" in, hoping
  57. * this will cause a diagnostic.
  58. */
  59. CHAR64LONG16 *block = (const CHAR64LONG16 *) buffer;
  60. #endif
  61. /* Copy context->state[] to working vars */
  62. a = state[0];
  63. b = state[1];
  64. c = state[2];
  65. d = state[3];
  66. e = state[4];
  67. /* 4 rounds of 20 operations each. Loop unrolled. */
  68. R0(a, b, c, d, e, 0);
  69. R0(e, a, b, c, d, 1);
  70. R0(d, e, a, b, c, 2);
  71. R0(c, d, e, a, b, 3);
  72. R0(b, c, d, e, a, 4);
  73. R0(a, b, c, d, e, 5);
  74. R0(e, a, b, c, d, 6);
  75. R0(d, e, a, b, c, 7);
  76. R0(c, d, e, a, b, 8);
  77. R0(b, c, d, e, a, 9);
  78. R0(a, b, c, d, e, 10);
  79. R0(e, a, b, c, d, 11);
  80. R0(d, e, a, b, c, 12);
  81. R0(c, d, e, a, b, 13);
  82. R0(b, c, d, e, a, 14);
  83. R0(a, b, c, d, e, 15);
  84. R1(e, a, b, c, d, 16);
  85. R1(d, e, a, b, c, 17);
  86. R1(c, d, e, a, b, 18);
  87. R1(b, c, d, e, a, 19);
  88. R2(a, b, c, d, e, 20);
  89. R2(e, a, b, c, d, 21);
  90. R2(d, e, a, b, c, 22);
  91. R2(c, d, e, a, b, 23);
  92. R2(b, c, d, e, a, 24);
  93. R2(a, b, c, d, e, 25);
  94. R2(e, a, b, c, d, 26);
  95. R2(d, e, a, b, c, 27);
  96. R2(c, d, e, a, b, 28);
  97. R2(b, c, d, e, a, 29);
  98. R2(a, b, c, d, e, 30);
  99. R2(e, a, b, c, d, 31);
  100. R2(d, e, a, b, c, 32);
  101. R2(c, d, e, a, b, 33);
  102. R2(b, c, d, e, a, 34);
  103. R2(a, b, c, d, e, 35);
  104. R2(e, a, b, c, d, 36);
  105. R2(d, e, a, b, c, 37);
  106. R2(c, d, e, a, b, 38);
  107. R2(b, c, d, e, a, 39);
  108. R3(a, b, c, d, e, 40);
  109. R3(e, a, b, c, d, 41);
  110. R3(d, e, a, b, c, 42);
  111. R3(c, d, e, a, b, 43);
  112. R3(b, c, d, e, a, 44);
  113. R3(a, b, c, d, e, 45);
  114. R3(e, a, b, c, d, 46);
  115. R3(d, e, a, b, c, 47);
  116. R3(c, d, e, a, b, 48);
  117. R3(b, c, d, e, a, 49);
  118. R3(a, b, c, d, e, 50);
  119. R3(e, a, b, c, d, 51);
  120. R3(d, e, a, b, c, 52);
  121. R3(c, d, e, a, b, 53);
  122. R3(b, c, d, e, a, 54);
  123. R3(a, b, c, d, e, 55);
  124. R3(e, a, b, c, d, 56);
  125. R3(d, e, a, b, c, 57);
  126. R3(c, d, e, a, b, 58);
  127. R3(b, c, d, e, a, 59);
  128. R4(a, b, c, d, e, 60);
  129. R4(e, a, b, c, d, 61);
  130. R4(d, e, a, b, c, 62);
  131. R4(c, d, e, a, b, 63);
  132. R4(b, c, d, e, a, 64);
  133. R4(a, b, c, d, e, 65);
  134. R4(e, a, b, c, d, 66);
  135. R4(d, e, a, b, c, 67);
  136. R4(c, d, e, a, b, 68);
  137. R4(b, c, d, e, a, 69);
  138. R4(a, b, c, d, e, 70);
  139. R4(e, a, b, c, d, 71);
  140. R4(d, e, a, b, c, 72);
  141. R4(c, d, e, a, b, 73);
  142. R4(b, c, d, e, a, 74);
  143. R4(a, b, c, d, e, 75);
  144. R4(e, a, b, c, d, 76);
  145. R4(d, e, a, b, c, 77);
  146. R4(c, d, e, a, b, 78);
  147. R4(b, c, d, e, a, 79);
  148. /* Add the working vars back into context.state[] */
  149. state[0] += a;
  150. state[1] += b;
  151. state[2] += c;
  152. state[3] += d;
  153. state[4] += e;
  154. /* Wipe variables */
  155. a = b = c = d = e = 0;
  156. #ifdef SHA1HANDSOFF
  157. memset(block, '\0', sizeof(block));
  158. #endif
  159. }
  160. /* HV_SHA1Init - Initialize new context */
  161. void HV_SHA1Init(
  162. HV_SHA1_CTX * context
  163. )
  164. {
  165. /* SHA1 initialization constants */
  166. context->state[0] = 0x67452301;
  167. context->state[1] = 0xEFCDAB89;
  168. context->state[2] = 0x98BADCFE;
  169. context->state[3] = 0x10325476;
  170. context->state[4] = 0xC3D2E1F0;
  171. context->count[0] = context->count[1] = 0;
  172. }
  173. /* Run your data through this. */
  174. void HV_SHA1Update(
  175. HV_SHA1_CTX * context,
  176. const unsigned char *data,
  177. uint32_t len
  178. )
  179. {
  180. uint32_t i;
  181. uint32_t j;
  182. j = context->count[0];
  183. if ((context->count[0] += len << 3) < j)
  184. context->count[1]++;
  185. context->count[1] += (len >> 29);
  186. j = (j >> 3) & 63;
  187. if ((j + len) > 63)
  188. {
  189. memcpy(&context->buffer[j], data, (i = 64 - j));
  190. HV_SHA1Transform(context->state, context->buffer);
  191. for (; i + 63 < len; i += 64)
  192. {
  193. HV_SHA1Transform(context->state, &data[i]);
  194. }
  195. j = 0;
  196. }
  197. else
  198. i = 0;
  199. memcpy(&context->buffer[j], &data[i], len - i);
  200. }
  201. /* Add padding and return the message digest. */
  202. void HV_SHA1Final(
  203. unsigned char digest[20],
  204. HV_SHA1_CTX * context
  205. )
  206. {
  207. unsigned i;
  208. unsigned char finalcount[8];
  209. unsigned char c;
  210. #if 0 /* untested "improvement" by DHR */
  211. /* Convert context->count to a sequence of bytes
  212. * in finalcount. Second element first, but
  213. * big-endian order within element.
  214. * But we do it all backwards.
  215. */
  216. unsigned char *fcp = &finalcount[8];
  217. for (i = 0; i < 2; i++)
  218. {
  219. uint32_t t = context->count[i];
  220. int j;
  221. for (j = 0; j < 4; t >>= 8, j++)
  222. *--fcp = (unsigned char) t}
  223. #else
  224. for (i = 0; i < 8; i++)
  225. {
  226. finalcount[i] = (unsigned char) ((context->count[(i >= 4 ? 0 : 1)] >> ((3 - (i & 3)) * 8)) & 255); /* Endian independent */
  227. }
  228. #endif
  229. c = 0200;
  230. HV_SHA1Update(context, &c, 1);
  231. while ((context->count[0] & 504) != 448)
  232. {
  233. c = 0000;
  234. HV_SHA1Update(context, &c, 1);
  235. }
  236. HV_SHA1Update(context, finalcount, 8); /* Should cause a HV_SHA1Transform() */
  237. for (i = 0; i < 20; i++)
  238. {
  239. digest[i] = (unsigned char)
  240. ((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
  241. }
  242. /* Wipe variables */
  243. memset(context, '\0', sizeof(*context));
  244. memset(&finalcount, '\0', sizeof(finalcount));
  245. }
  246. void HV_SHA1(
  247. char *hash_out,
  248. const char *str,
  249. uint32_t len)
  250. {
  251. HV_SHA1_CTX ctx;
  252. unsigned int ii;
  253. HV_SHA1Init(&ctx);
  254. for (ii=0; ii<len; ii+=1)
  255. HV_SHA1Update(&ctx, (const unsigned char*)str + ii, 1);
  256. HV_SHA1Final((unsigned char *)hash_out, &ctx);
  257. hash_out[20] = '\0';
  258. }
  259. void hv_sha1(unsigned char* input, uint32_t inputlen, unsigned char digest[20]) {
  260. HV_SHA1_CTX ctx;
  261. HV_SHA1Init(&ctx);
  262. HV_SHA1Update(&ctx, input, inputlen);
  263. HV_SHA1Final(digest, &ctx);
  264. }
  265. static inline char i2hex(unsigned char i) {
  266. return i < 10 ? i + '0' : i - 10 + 'a';
  267. }
  268. void hv_sha1_hex(unsigned char* input, uint32_t inputlen, char* output, uint32_t outputlen) {
  269. int i;
  270. unsigned char digest[20];
  271. if (outputlen < 40) return;
  272. hv_sha1(input, inputlen, digest);
  273. for (i = 0; i < 20; ++i) {
  274. *output++ = i2hex(digest[i] >> 4);
  275. *output++ = i2hex(digest[i] & 0x0F);
  276. }
  277. if (outputlen > 40) *output = '\0';
  278. }