sha1.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Stefan Esser <sesser@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* This code is heavily based on the PHP md5 implementation */
  19. #include <string.h>
  20. #include "sha1.h"
  21. #include "../md5/md5.h"
  22. void make_sha1_digest(char *sha1str, unsigned char *digest)
  23. {
  24. make_digest_ex(sha1str, digest, 20);
  25. }
  26. void sha1(void *data, size_t data_len, void *hash)
  27. {
  28. SHA1_CTX context;
  29. unsigned char digest[20];
  30. SHA1Init(&context);
  31. SHA1Update(&context, (unsigned char *) data, data_len);
  32. SHA1Final(digest, &context);
  33. make_digest_ex(hash, digest, 20);
  34. }
  35. static void SHA1Transform(uint32_t[5], const unsigned char[64]);
  36. static void SHA1Encode(unsigned char *, uint32_t *, unsigned int);
  37. static void SHA1Decode(uint32_t *, const unsigned char *, unsigned int);
  38. static const unsigned char PADDING[64] =
  39. {
  40. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  41. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  42. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  43. };
  44. /* F, G, H and I are basic SHA1 functions.
  45. */
  46. #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
  47. #define G(x, y, z) ((x) ^ (y) ^ (z))
  48. #define H(x, y, z) (((x) & (y)) | ((z) & ((x) | (y))))
  49. #define I(x, y, z) ((x) ^ (y) ^ (z))
  50. /* ROTATE_LEFT rotates x left n bits.
  51. */
  52. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  53. /* W[i]
  54. */
  55. #define W(i) ( tmp=x[(i-3)&15]^x[(i-8)&15]^x[(i-14)&15]^x[i&15], \
  56. (x[i&15]=ROTATE_LEFT(tmp, 1)) )
  57. /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  58. */
  59. #define FF(a, b, c, d, e, w) { \
  60. (e) += F ((b), (c), (d)) + (w) + (uint32_t)(0x5A827999); \
  61. (e) += ROTATE_LEFT ((a), 5); \
  62. (b) = ROTATE_LEFT((b), 30); \
  63. }
  64. #define GG(a, b, c, d, e, w) { \
  65. (e) += G ((b), (c), (d)) + (w) + (uint32_t)(0x6ED9EBA1); \
  66. (e) += ROTATE_LEFT ((a), 5); \
  67. (b) = ROTATE_LEFT((b), 30); \
  68. }
  69. #define HH(a, b, c, d, e, w) { \
  70. (e) += H ((b), (c), (d)) + (w) + (uint32_t)(0x8F1BBCDC); \
  71. (e) += ROTATE_LEFT ((a), 5); \
  72. (b) = ROTATE_LEFT((b), 30); \
  73. }
  74. #define II(a, b, c, d, e, w) { \
  75. (e) += I ((b), (c), (d)) + (w) + (uint32_t)(0xCA62C1D6); \
  76. (e) += ROTATE_LEFT ((a), 5); \
  77. (b) = ROTATE_LEFT((b), 30); \
  78. }
  79. void SHA1Init(SHA1_CTX * context)
  80. {
  81. context->count[0] = context->count[1] = 0;
  82. /* Load magic initialization constants.
  83. */
  84. context->state[0] = 0x67452301;
  85. context->state[1] = 0xefcdab89;
  86. context->state[2] = 0x98badcfe;
  87. context->state[3] = 0x10325476;
  88. context->state[4] = 0xc3d2e1f0;
  89. }
  90. void SHA1Update(SHA1_CTX * context, const unsigned char *input,
  91. size_t inputLen)
  92. {
  93. unsigned int i, index, partLen;
  94. /* Compute number of bytes mod 64 */
  95. index = (unsigned int) ((context->count[0] >> 3) & 0x3F);
  96. /* Update number of bits */
  97. if ((context->count[0] += ((uint32_t) inputLen << 3))
  98. < ((uint32_t) inputLen << 3))
  99. context->count[1]++;
  100. context->count[1] += ((uint32_t) inputLen >> 29);
  101. partLen = 64 - index;
  102. /* Transform as many times as possible.
  103. */
  104. if (inputLen >= partLen) {
  105. memcpy
  106. ((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
  107. SHA1Transform(context->state, context->buffer);
  108. for (i = partLen; i + 63 < inputLen; i += 64)
  109. SHA1Transform(context->state, &input[i]);
  110. index = 0;
  111. } else
  112. i = 0;
  113. /* Buffer remaining input */
  114. memcpy
  115. ((unsigned char*) & context->buffer[index], (unsigned char*) & input[i],
  116. inputLen - i);
  117. }
  118. void SHA1Final(unsigned char digest[20], SHA1_CTX * context)
  119. {
  120. unsigned char bits[8];
  121. unsigned int index, padLen;
  122. /* Save number of bits */
  123. bits[7] = context->count[0] & 0xFF;
  124. bits[6] = (context->count[0] >> 8) & 0xFF;
  125. bits[5] = (context->count[0] >> 16) & 0xFF;
  126. bits[4] = (context->count[0] >> 24) & 0xFF;
  127. bits[3] = context->count[1] & 0xFF;
  128. bits[2] = (context->count[1] >> 8) & 0xFF;
  129. bits[1] = (context->count[1] >> 16) & 0xFF;
  130. bits[0] = (context->count[1] >> 24) & 0xFF;
  131. /* Pad out to 56 mod 64.
  132. */
  133. index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
  134. padLen = (index < 56) ? (56 - index) : (120 - index);
  135. SHA1Update(context, PADDING, padLen);
  136. /* Append length (before padding) */
  137. SHA1Update(context, bits, 8);
  138. /* Store state in digest */
  139. SHA1Encode(digest, context->state, 20);
  140. /* Zeroize sensitive information.
  141. */
  142. memset((unsigned char*) context, 0, sizeof(*context));
  143. }
  144. /* {{{ SHA1Transform
  145. * SHA1 basic transformation. Transforms state based on block.
  146. */
  147. static void SHA1Transform(state, block)
  148. uint32_t state[5];
  149. const unsigned char block[64];
  150. {
  151. uint32_t a = state[0], b = state[1], c = state[2];
  152. uint32_t d = state[3], e = state[4], x[16], tmp;
  153. SHA1Decode(x, block, 64);
  154. /* Round 1 */
  155. FF(a, b, c, d, e, x[0]); /* 1 */
  156. FF(e, a, b, c, d, x[1]); /* 2 */
  157. FF(d, e, a, b, c, x[2]); /* 3 */
  158. FF(c, d, e, a, b, x[3]); /* 4 */
  159. FF(b, c, d, e, a, x[4]); /* 5 */
  160. FF(a, b, c, d, e, x[5]); /* 6 */
  161. FF(e, a, b, c, d, x[6]); /* 7 */
  162. FF(d, e, a, b, c, x[7]); /* 8 */
  163. FF(c, d, e, a, b, x[8]); /* 9 */
  164. FF(b, c, d, e, a, x[9]); /* 10 */
  165. FF(a, b, c, d, e, x[10]); /* 11 */
  166. FF(e, a, b, c, d, x[11]); /* 12 */
  167. FF(d, e, a, b, c, x[12]); /* 13 */
  168. FF(c, d, e, a, b, x[13]); /* 14 */
  169. FF(b, c, d, e, a, x[14]); /* 15 */
  170. FF(a, b, c, d, e, x[15]); /* 16 */
  171. FF(e, a, b, c, d, W(16)); /* 17 */
  172. FF(d, e, a, b, c, W(17)); /* 18 */
  173. FF(c, d, e, a, b, W(18)); /* 19 */
  174. FF(b, c, d, e, a, W(19)); /* 20 */
  175. /* Round 2 */
  176. GG(a, b, c, d, e, W(20)); /* 21 */
  177. GG(e, a, b, c, d, W(21)); /* 22 */
  178. GG(d, e, a, b, c, W(22)); /* 23 */
  179. GG(c, d, e, a, b, W(23)); /* 24 */
  180. GG(b, c, d, e, a, W(24)); /* 25 */
  181. GG(a, b, c, d, e, W(25)); /* 26 */
  182. GG(e, a, b, c, d, W(26)); /* 27 */
  183. GG(d, e, a, b, c, W(27)); /* 28 */
  184. GG(c, d, e, a, b, W(28)); /* 29 */
  185. GG(b, c, d, e, a, W(29)); /* 30 */
  186. GG(a, b, c, d, e, W(30)); /* 31 */
  187. GG(e, a, b, c, d, W(31)); /* 32 */
  188. GG(d, e, a, b, c, W(32)); /* 33 */
  189. GG(c, d, e, a, b, W(33)); /* 34 */
  190. GG(b, c, d, e, a, W(34)); /* 35 */
  191. GG(a, b, c, d, e, W(35)); /* 36 */
  192. GG(e, a, b, c, d, W(36)); /* 37 */
  193. GG(d, e, a, b, c, W(37)); /* 38 */
  194. GG(c, d, e, a, b, W(38)); /* 39 */
  195. GG(b, c, d, e, a, W(39)); /* 40 */
  196. /* Round 3 */
  197. HH(a, b, c, d, e, W(40)); /* 41 */
  198. HH(e, a, b, c, d, W(41)); /* 42 */
  199. HH(d, e, a, b, c, W(42)); /* 43 */
  200. HH(c, d, e, a, b, W(43)); /* 44 */
  201. HH(b, c, d, e, a, W(44)); /* 45 */
  202. HH(a, b, c, d, e, W(45)); /* 46 */
  203. HH(e, a, b, c, d, W(46)); /* 47 */
  204. HH(d, e, a, b, c, W(47)); /* 48 */
  205. HH(c, d, e, a, b, W(48)); /* 49 */
  206. HH(b, c, d, e, a, W(49)); /* 50 */
  207. HH(a, b, c, d, e, W(50)); /* 51 */
  208. HH(e, a, b, c, d, W(51)); /* 52 */
  209. HH(d, e, a, b, c, W(52)); /* 53 */
  210. HH(c, d, e, a, b, W(53)); /* 54 */
  211. HH(b, c, d, e, a, W(54)); /* 55 */
  212. HH(a, b, c, d, e, W(55)); /* 56 */
  213. HH(e, a, b, c, d, W(56)); /* 57 */
  214. HH(d, e, a, b, c, W(57)); /* 58 */
  215. HH(c, d, e, a, b, W(58)); /* 59 */
  216. HH(b, c, d, e, a, W(59)); /* 60 */
  217. /* Round 4 */
  218. II(a, b, c, d, e, W(60)); /* 61 */
  219. II(e, a, b, c, d, W(61)); /* 62 */
  220. II(d, e, a, b, c, W(62)); /* 63 */
  221. II(c, d, e, a, b, W(63)); /* 64 */
  222. II(b, c, d, e, a, W(64)); /* 65 */
  223. II(a, b, c, d, e, W(65)); /* 66 */
  224. II(e, a, b, c, d, W(66)); /* 67 */
  225. II(d, e, a, b, c, W(67)); /* 68 */
  226. II(c, d, e, a, b, W(68)); /* 69 */
  227. II(b, c, d, e, a, W(69)); /* 70 */
  228. II(a, b, c, d, e, W(70)); /* 71 */
  229. II(e, a, b, c, d, W(71)); /* 72 */
  230. II(d, e, a, b, c, W(72)); /* 73 */
  231. II(c, d, e, a, b, W(73)); /* 74 */
  232. II(b, c, d, e, a, W(74)); /* 75 */
  233. II(a, b, c, d, e, W(75)); /* 76 */
  234. II(e, a, b, c, d, W(76)); /* 77 */
  235. II(d, e, a, b, c, W(77)); /* 78 */
  236. II(c, d, e, a, b, W(78)); /* 79 */
  237. II(b, c, d, e, a, W(79)); /* 80 */
  238. state[0] += a;
  239. state[1] += b;
  240. state[2] += c;
  241. state[3] += d;
  242. state[4] += e;
  243. /* Zeroize sensitive information. */
  244. memset((unsigned char*) x, 0, sizeof(x));
  245. }
  246. /* }}} */
  247. /* {{{ SHA1Encode
  248. Encodes input (uint32_t) into output (unsigned char). Assumes len is
  249. a multiple of 4.
  250. */
  251. static void SHA1Encode(output, input, len)
  252. unsigned char *output;
  253. uint32_t *input;
  254. unsigned int len;
  255. {
  256. unsigned int i, j;
  257. for (i = 0, j = 0; j < len; i++, j += 4) {
  258. output[j] = (unsigned char) ((input[i] >> 24) & 0xff);
  259. output[j + 1] = (unsigned char) ((input[i] >> 16) & 0xff);
  260. output[j + 2] = (unsigned char) ((input[i] >> 8) & 0xff);
  261. output[j + 3] = (unsigned char) (input[i] & 0xff);
  262. }
  263. }
  264. /* }}} */
  265. /* {{{ SHA1Decode
  266. Decodes input (unsigned char) into output (uint32_t). Assumes len is
  267. a multiple of 4.
  268. */
  269. static void SHA1Decode(output, input, len)
  270. uint32_t *output;
  271. const unsigned char *input;
  272. unsigned int len;
  273. {
  274. unsigned int i, j;
  275. for (i = 0, j = 0; j < len; i++, j += 4)
  276. output[i] = ((uint32_t) input[j + 3]) | (((uint32_t) input[j + 2]) << 8) |
  277. (((uint32_t) input[j + 1]) << 16) | (((uint32_t) input[j]) << 24);
  278. }
  279. /* }}} */
  280. /*
  281. * Local variables:
  282. * tab-width: 4
  283. * c-basic-offset: 4
  284. * End:
  285. * vim600: sw=4 ts=4 fdm=marker
  286. * vim<600: sw=4 ts=4
  287. */