Verify bittorrent .torrent metainfo files.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

297 lines
7.5KB

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