sha1.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /**
  2. \file
  3. SHA-1 in C
  4. By Steve Reid <sreid@sea-to-sky.net>
  5. 100% Public Domain
  6. -----------------
  7. Modified 7/98
  8. By James H. Brown <jbrown@burgoyne.com>
  9. Still 100% Public Domain
  10. Corrected a problem which generated improper hash values on 16 bit machines
  11. Routine mono_sha1_update changed from
  12. void mono_sha1_update(Il2CppSHA1Context* context, unsigned char* data, unsigned int
  13. len)
  14. to
  15. void mono_sha1_update(Il2CppSHA1Context* context, unsigned char* data, unsigned
  16. long len)
  17. The 'len' parameter was declared an int which works fine on 32 bit machines.
  18. However, on 16 bit machines an int is too small for the shifts being done
  19. against
  20. it. This caused the hash function to generate incorrect values if len was
  21. greater than 8191 (8K - 1) due to the 'len << 3' on line 3 of mono_sha1_update().
  22. Since the file IO in main() reads 16K at a time, any file 8K or larger would
  23. be guaranteed to generate the wrong hash (e.g. Test Vector #3, a million
  24. "a"s).
  25. I also changed the declaration of variables i & j in mono_sha1_update to
  26. unsigned long from unsigned int for the same reason.
  27. These changes should make no difference to any 32 bit implementations since
  28. an
  29. int and a long are the same size in those environments.
  30. --
  31. I also corrected a few compiler warnings generated by Borland C.
  32. 1. Added #include <process.h> for exit() prototype
  33. 2. Removed unused variable 'j' in mono_sha1_final
  34. 3. Changed exit(0) to return(0) at end of main.
  35. ALL changes I made can be located by searching for comments containing 'JHB'
  36. -----------------
  37. Modified 8/98
  38. By Steve Reid <sreid@sea-to-sky.net>
  39. Still 100% public domain
  40. 1- Removed #include <process.h> and used return() instead of exit()
  41. 2- Fixed overwriting of finalcount in mono_sha1_final() (discovered by Chris Hall)
  42. 3- Changed email address from steve@edmweb.com to sreid@sea-to-sky.net
  43. -----------------
  44. Modified 4/01
  45. By Saul Kravitz <Saul.Kravitz@celera.com>
  46. Still 100% PD
  47. Modified to run on Compaq Alpha hardware.
  48. */
  49. /*
  50. Test Vectors (from FIPS PUB 180-1)
  51. "abc"
  52. A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
  53. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
  54. 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
  55. A million repetitions of "a"
  56. 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
  57. */
  58. #include <stdio.h>
  59. #include <string.h>
  60. #include <stdint.h>
  61. typedef struct
  62. {
  63. uint32_t state[5];
  64. uint32_t count[2];
  65. unsigned char buffer[64];
  66. } Il2CppSHA1Context;
  67. /* #include <process.h> */ /* prototype for exit() - JHB */
  68. /* Using return() instead of exit() - SWR */
  69. static void SHA1Transform(uint32_t state[5], const uint8_t buffer[64]);
  70. #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
  71. /* blk0() and blk() perform the initial expand. */
  72. /* I got the idea of expanding during the round function from SSLeay */
  73. #if G_BYTE_ORDER == G_LITTLE_ENDIAN
  74. #define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
  75. |(rol(block->l[i],8)&0x00FF00FF))
  76. #else
  77. #define blk0(i) block->l[i]
  78. #endif
  79. #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
  80. ^block->l[(i+2)&15]^block->l[i&15],1))
  81. /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
  82. #define R0(v, w, x, y, z, i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
  83. #define R1(v, w, x, y, z, i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
  84. #define R2(v, w, x, y, z, i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
  85. #define R3(v, w, x, y, z, i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
  86. #define R4(v, w, x, y, z, i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
  87. /* Hash a single 512-bit block. This is the core of the algorithm. */
  88. static void SHA1Transform(uint32_t state[5], const uint8_t buffer[64])
  89. {
  90. uint32_t a, b, c, d, e;
  91. typedef union
  92. {
  93. unsigned char c[64];
  94. uint32_t l[16];
  95. } CHAR64LONG16;
  96. CHAR64LONG16* block;
  97. unsigned char workspace[64];
  98. block = (CHAR64LONG16*)workspace;
  99. memcpy(block, buffer, 64);
  100. /* Copy context->state[] to working vars */
  101. a = state[0];
  102. b = state[1];
  103. c = state[2];
  104. d = state[3];
  105. e = state[4];
  106. /* 4 rounds of 20 operations each. Loop unrolled. */
  107. R0(a, b, c, d, e, 0); R0(e, a, b, c, d, 1); R0(d, e, a, b, c, 2); R0(c, d, e, a, b, 3);
  108. R0(b, c, d, e, a, 4); R0(a, b, c, d, e, 5); R0(e, a, b, c, d, 6); R0(d, e, a, b, c, 7);
  109. R0(c, d, e, a, b, 8); R0(b, c, d, e, a, 9); R0(a, b, c, d, e, 10); R0(e, a, b, c, d, 11);
  110. R0(d, e, a, b, c, 12); R0(c, d, e, a, b, 13); R0(b, c, d, e, a, 14); R0(a, b, c, d, e, 15);
  111. R1(e, a, b, c, d, 16); R1(d, e, a, b, c, 17); R1(c, d, e, a, b, 18); R1(b, c, d, e, a, 19);
  112. R2(a, b, c, d, e, 20); R2(e, a, b, c, d, 21); R2(d, e, a, b, c, 22); R2(c, d, e, a, b, 23);
  113. R2(b, c, d, e, a, 24); R2(a, b, c, d, e, 25); R2(e, a, b, c, d, 26); R2(d, e, a, b, c, 27);
  114. R2(c, d, e, a, b, 28); R2(b, c, d, e, a, 29); R2(a, b, c, d, e, 30); R2(e, a, b, c, d, 31);
  115. R2(d, e, a, b, c, 32); R2(c, d, e, a, b, 33); R2(b, c, d, e, a, 34); R2(a, b, c, d, e, 35);
  116. R2(e, a, b, c, d, 36); R2(d, e, a, b, c, 37); R2(c, d, e, a, b, 38); R2(b, c, d, e, a, 39);
  117. R3(a, b, c, d, e, 40); R3(e, a, b, c, d, 41); R3(d, e, a, b, c, 42); R3(c, d, e, a, b, 43);
  118. R3(b, c, d, e, a, 44); R3(a, b, c, d, e, 45); R3(e, a, b, c, d, 46); R3(d, e, a, b, c, 47);
  119. R3(c, d, e, a, b, 48); R3(b, c, d, e, a, 49); R3(a, b, c, d, e, 50); R3(e, a, b, c, d, 51);
  120. R3(d, e, a, b, c, 52); R3(c, d, e, a, b, 53); R3(b, c, d, e, a, 54); R3(a, b, c, d, e, 55);
  121. R3(e, a, b, c, d, 56); R3(d, e, a, b, c, 57); R3(c, d, e, a, b, 58); R3(b, c, d, e, a, 59);
  122. R4(a, b, c, d, e, 60); R4(e, a, b, c, d, 61); R4(d, e, a, b, c, 62); R4(c, d, e, a, b, 63);
  123. R4(b, c, d, e, a, 64); R4(a, b, c, d, e, 65); R4(e, a, b, c, d, 66); R4(d, e, a, b, c, 67);
  124. R4(c, d, e, a, b, 68); R4(b, c, d, e, a, 69); R4(a, b, c, d, e, 70); R4(e, a, b, c, d, 71);
  125. R4(d, e, a, b, c, 72); R4(c, d, e, a, b, 73); R4(b, c, d, e, a, 74); R4(a, b, c, d, e, 75);
  126. R4(e, a, b, c, d, 76); R4(d, e, a, b, c, 77); R4(c, d, e, a, b, 78); R4(b, c, d, e, a, 79);
  127. /* Add the working vars back into context.state[] */
  128. state[0] += a;
  129. state[1] += b;
  130. state[2] += c;
  131. state[3] += d;
  132. state[4] += e;
  133. /* Wipe variables */
  134. a = b = c = d = e = 0;
  135. }
  136. /* sha1_init - Initialize new context */
  137. static void sha1_init(Il2CppSHA1Context* context)
  138. {
  139. /* SHA1 initialization constants */
  140. context->state[0] = 0x67452301;
  141. context->state[1] = 0xEFCDAB89;
  142. context->state[2] = 0x98BADCFE;
  143. context->state[3] = 0x10325476;
  144. context->state[4] = 0xC3D2E1F0;
  145. context->count[0] = context->count[1] = 0;
  146. }
  147. /* Run your data through this. */
  148. static void sha1_update(Il2CppSHA1Context* context, const uint8_t* data, uint32_t len) /*
  149. JHB */
  150. {
  151. uint32_t i, j; /* JHB */
  152. #ifdef VERBOSE
  153. SHAPrintContext(context, "before");
  154. #endif
  155. j = (context->count[0] >> 3) & 63;
  156. if ((context->count[0] += len << 3) < (len << 3)) context->count[1]++;
  157. context->count[1] += (len >> 29);
  158. if ((j + len) > 63)
  159. {
  160. memcpy(&context->buffer[j], data, (i = 64 - j));
  161. SHA1Transform(context->state, context->buffer);
  162. for (; i + 63 < len; i += 64)
  163. {
  164. SHA1Transform(context->state, &data[i]);
  165. }
  166. j = 0;
  167. }
  168. else i = 0;
  169. memcpy(&context->buffer[j], &data[i], len - i);
  170. #ifdef VERBOSE
  171. SHAPrintContext(context, "after ");
  172. #endif
  173. }
  174. /* Add padding and return the message digest. */
  175. static void sha1_final(Il2CppSHA1Context* context, unsigned char digest[20])
  176. {
  177. uint32_t i; /* JHB */
  178. unsigned char finalcount[8];
  179. for (i = 0; i < 8; i++)
  180. {
  181. finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)]
  182. >> ((3 - (i & 3)) * 8)) & 255); /* Endian independent */
  183. }
  184. sha1_update(context, (unsigned char *)"\200", 1);
  185. while ((context->count[0] & 504) != 448)
  186. {
  187. sha1_update(context, (unsigned char *)"\0", 1);
  188. }
  189. sha1_update(context, finalcount, 8); /* Should cause a SHA1Transform()
  190. */
  191. for (i = 0; i < 20; i++)
  192. {
  193. digest[i] = (unsigned char)
  194. ((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
  195. }
  196. /* Wipe variables */
  197. i = 0; /* JHB */
  198. memset(context->buffer, 0, 64);
  199. memset(context->state, 0, 20);
  200. memset(context->count, 0, 8);
  201. memset(finalcount, 0, 8); /* SWR */
  202. SHA1Transform(context->state, context->buffer);
  203. }
  204. /**
  205. * sha1_get_digest:
  206. */
  207. void sha1_get_digest(const uint8_t *buffer, int buffer_size, uint8_t digest[20])
  208. {
  209. Il2CppSHA1Context ctx;
  210. sha1_init(&ctx);
  211. sha1_update(&ctx, buffer, buffer_size);
  212. sha1_final(&ctx, digest);
  213. }