sha1.inl 8.6 KB

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