sha1.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Stefan Esser <sesser@php.net> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #include "php.h"
  17. /* This code is heavily based on the PHP md5 implementation */
  18. #include "sha1.h"
  19. #include "md5.h"
  20. PHPAPI void make_sha1_digest(char *sha1str, const unsigned char *digest)
  21. {
  22. make_digest_ex(sha1str, digest, 20);
  23. }
  24. /* {{{ Calculate the sha1 hash of a string */
  25. PHP_FUNCTION(sha1)
  26. {
  27. zend_string *arg;
  28. bool raw_output = 0;
  29. PHP_SHA1_CTX context;
  30. unsigned char digest[20];
  31. ZEND_PARSE_PARAMETERS_START(1, 2)
  32. Z_PARAM_STR(arg)
  33. Z_PARAM_OPTIONAL
  34. Z_PARAM_BOOL(raw_output)
  35. ZEND_PARSE_PARAMETERS_END();
  36. PHP_SHA1Init(&context);
  37. PHP_SHA1Update(&context, (unsigned char *) ZSTR_VAL(arg), ZSTR_LEN(arg));
  38. PHP_SHA1Final(digest, &context);
  39. if (raw_output) {
  40. RETURN_STRINGL((char *) digest, 20);
  41. } else {
  42. RETVAL_NEW_STR(zend_string_alloc(40, 0));
  43. make_digest_ex(Z_STRVAL_P(return_value), digest, 20);
  44. }
  45. }
  46. /* }}} */
  47. /* {{{ Calculate the sha1 hash of given filename */
  48. PHP_FUNCTION(sha1_file)
  49. {
  50. char *arg;
  51. size_t arg_len;
  52. bool raw_output = 0;
  53. unsigned char buf[1024];
  54. unsigned char digest[20];
  55. PHP_SHA1_CTX context;
  56. ssize_t n;
  57. php_stream *stream;
  58. ZEND_PARSE_PARAMETERS_START(1, 2)
  59. Z_PARAM_PATH(arg, arg_len)
  60. Z_PARAM_OPTIONAL
  61. Z_PARAM_BOOL(raw_output)
  62. ZEND_PARSE_PARAMETERS_END();
  63. stream = php_stream_open_wrapper(arg, "rb", REPORT_ERRORS, NULL);
  64. if (!stream) {
  65. RETURN_FALSE;
  66. }
  67. PHP_SHA1Init(&context);
  68. while ((n = php_stream_read(stream, (char *) buf, sizeof(buf))) > 0) {
  69. PHP_SHA1Update(&context, buf, n);
  70. }
  71. PHP_SHA1Final(digest, &context);
  72. php_stream_close(stream);
  73. if (raw_output) {
  74. RETURN_STRINGL((char *) digest, 20);
  75. } else {
  76. RETVAL_NEW_STR(zend_string_alloc(40, 0));
  77. make_digest_ex(Z_STRVAL_P(return_value), digest, 20);
  78. }
  79. }
  80. /* }}} */
  81. static void SHA1Transform(uint32_t[5], const unsigned char[64]);
  82. static void SHA1Encode(unsigned char *, uint32_t *, unsigned int);
  83. static void SHA1Decode(uint32_t *, const unsigned char *, unsigned int);
  84. static const unsigned char PADDING[64] =
  85. {
  86. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  87. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  88. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  89. };
  90. /* F, G, H and I are basic SHA1 functions.
  91. */
  92. #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
  93. #define G(x, y, z) ((x) ^ (y) ^ (z))
  94. #define H(x, y, z) (((x) & (y)) | ((z) & ((x) | (y))))
  95. #define I(x, y, z) ((x) ^ (y) ^ (z))
  96. /* ROTATE_LEFT rotates x left n bits.
  97. */
  98. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  99. /* W[i]
  100. */
  101. #define W(i) ( tmp=x[(i-3)&15]^x[(i-8)&15]^x[(i-14)&15]^x[i&15], \
  102. (x[i&15]=ROTATE_LEFT(tmp, 1)) )
  103. /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  104. */
  105. #define FF(a, b, c, d, e, w) { \
  106. (e) += F ((b), (c), (d)) + (w) + (uint32_t)(0x5A827999); \
  107. (e) += ROTATE_LEFT ((a), 5); \
  108. (b) = ROTATE_LEFT((b), 30); \
  109. }
  110. #define GG(a, b, c, d, e, w) { \
  111. (e) += G ((b), (c), (d)) + (w) + (uint32_t)(0x6ED9EBA1); \
  112. (e) += ROTATE_LEFT ((a), 5); \
  113. (b) = ROTATE_LEFT((b), 30); \
  114. }
  115. #define HH(a, b, c, d, e, w) { \
  116. (e) += H ((b), (c), (d)) + (w) + (uint32_t)(0x8F1BBCDC); \
  117. (e) += ROTATE_LEFT ((a), 5); \
  118. (b) = ROTATE_LEFT((b), 30); \
  119. }
  120. #define II(a, b, c, d, e, w) { \
  121. (e) += I ((b), (c), (d)) + (w) + (uint32_t)(0xCA62C1D6); \
  122. (e) += ROTATE_LEFT ((a), 5); \
  123. (b) = ROTATE_LEFT((b), 30); \
  124. }
  125. /* {{{ PHP_SHA1Init
  126. * SHA1 initialization. Begins an SHA1 operation, writing a new context.
  127. */
  128. PHPAPI void PHP_SHA1InitArgs(PHP_SHA1_CTX * context, ZEND_ATTRIBUTE_UNUSED HashTable *args)
  129. {
  130. context->count[0] = context->count[1] = 0;
  131. /* Load magic initialization constants.
  132. */
  133. context->state[0] = 0x67452301;
  134. context->state[1] = 0xefcdab89;
  135. context->state[2] = 0x98badcfe;
  136. context->state[3] = 0x10325476;
  137. context->state[4] = 0xc3d2e1f0;
  138. }
  139. /* }}} */
  140. /* {{{ PHP_SHA1Update
  141. SHA1 block update operation. Continues an SHA1 message-digest
  142. operation, processing another message block, and updating the
  143. context.
  144. */
  145. PHPAPI void PHP_SHA1Update(PHP_SHA1_CTX * context, const unsigned char *input,
  146. size_t inputLen)
  147. {
  148. unsigned int i, index, partLen;
  149. /* Compute number of bytes mod 64 */
  150. index = (unsigned int) ((context->count[0] >> 3) & 0x3F);
  151. /* Update number of bits */
  152. if ((context->count[0] += ((uint32_t) inputLen << 3))
  153. < ((uint32_t) inputLen << 3))
  154. context->count[1]++;
  155. context->count[1] += ((uint32_t) inputLen >> 29);
  156. partLen = 64 - index;
  157. /* Transform as many times as possible.
  158. */
  159. if (inputLen >= partLen) {
  160. memcpy
  161. ((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
  162. SHA1Transform(context->state, context->buffer);
  163. for (i = partLen; i + 63 < inputLen; i += 64)
  164. SHA1Transform(context->state, &input[i]);
  165. index = 0;
  166. } else
  167. i = 0;
  168. /* Buffer remaining input */
  169. memcpy
  170. ((unsigned char*) & context->buffer[index], (unsigned char*) & input[i],
  171. inputLen - i);
  172. }
  173. /* }}} */
  174. /* {{{ PHP_SHA1Final
  175. SHA1 finalization. Ends an SHA1 message-digest operation, writing the
  176. the message digest and zeroizing the context.
  177. */
  178. PHPAPI void PHP_SHA1Final(unsigned char digest[20], PHP_SHA1_CTX * context)
  179. {
  180. unsigned char bits[8];
  181. unsigned int index, padLen;
  182. /* Save number of bits */
  183. bits[7] = context->count[0] & 0xFF;
  184. bits[6] = (context->count[0] >> 8) & 0xFF;
  185. bits[5] = (context->count[0] >> 16) & 0xFF;
  186. bits[4] = (context->count[0] >> 24) & 0xFF;
  187. bits[3] = context->count[1] & 0xFF;
  188. bits[2] = (context->count[1] >> 8) & 0xFF;
  189. bits[1] = (context->count[1] >> 16) & 0xFF;
  190. bits[0] = (context->count[1] >> 24) & 0xFF;
  191. /* Pad out to 56 mod 64.
  192. */
  193. index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
  194. padLen = (index < 56) ? (56 - index) : (120 - index);
  195. PHP_SHA1Update(context, PADDING, padLen);
  196. /* Append length (before padding) */
  197. PHP_SHA1Update(context, bits, 8);
  198. /* Store state in digest */
  199. SHA1Encode(digest, context->state, 20);
  200. /* Zeroize sensitive information.
  201. */
  202. ZEND_SECURE_ZERO((unsigned char*) context, sizeof(*context));
  203. }
  204. /* }}} */
  205. /* {{{ SHA1Transform
  206. * SHA1 basic transformation. Transforms state based on block.
  207. */
  208. static void SHA1Transform(state, block)
  209. uint32_t state[5];
  210. const unsigned char block[64];
  211. {
  212. uint32_t a = state[0], b = state[1], c = state[2];
  213. uint32_t d = state[3], e = state[4], x[16], tmp;
  214. SHA1Decode(x, block, 64);
  215. /* Round 1 */
  216. FF(a, b, c, d, e, x[0]); /* 1 */
  217. FF(e, a, b, c, d, x[1]); /* 2 */
  218. FF(d, e, a, b, c, x[2]); /* 3 */
  219. FF(c, d, e, a, b, x[3]); /* 4 */
  220. FF(b, c, d, e, a, x[4]); /* 5 */
  221. FF(a, b, c, d, e, x[5]); /* 6 */
  222. FF(e, a, b, c, d, x[6]); /* 7 */
  223. FF(d, e, a, b, c, x[7]); /* 8 */
  224. FF(c, d, e, a, b, x[8]); /* 9 */
  225. FF(b, c, d, e, a, x[9]); /* 10 */
  226. FF(a, b, c, d, e, x[10]); /* 11 */
  227. FF(e, a, b, c, d, x[11]); /* 12 */
  228. FF(d, e, a, b, c, x[12]); /* 13 */
  229. FF(c, d, e, a, b, x[13]); /* 14 */
  230. FF(b, c, d, e, a, x[14]); /* 15 */
  231. FF(a, b, c, d, e, x[15]); /* 16 */
  232. FF(e, a, b, c, d, W(16)); /* 17 */
  233. FF(d, e, a, b, c, W(17)); /* 18 */
  234. FF(c, d, e, a, b, W(18)); /* 19 */
  235. FF(b, c, d, e, a, W(19)); /* 20 */
  236. /* Round 2 */
  237. GG(a, b, c, d, e, W(20)); /* 21 */
  238. GG(e, a, b, c, d, W(21)); /* 22 */
  239. GG(d, e, a, b, c, W(22)); /* 23 */
  240. GG(c, d, e, a, b, W(23)); /* 24 */
  241. GG(b, c, d, e, a, W(24)); /* 25 */
  242. GG(a, b, c, d, e, W(25)); /* 26 */
  243. GG(e, a, b, c, d, W(26)); /* 27 */
  244. GG(d, e, a, b, c, W(27)); /* 28 */
  245. GG(c, d, e, a, b, W(28)); /* 29 */
  246. GG(b, c, d, e, a, W(29)); /* 30 */
  247. GG(a, b, c, d, e, W(30)); /* 31 */
  248. GG(e, a, b, c, d, W(31)); /* 32 */
  249. GG(d, e, a, b, c, W(32)); /* 33 */
  250. GG(c, d, e, a, b, W(33)); /* 34 */
  251. GG(b, c, d, e, a, W(34)); /* 35 */
  252. GG(a, b, c, d, e, W(35)); /* 36 */
  253. GG(e, a, b, c, d, W(36)); /* 37 */
  254. GG(d, e, a, b, c, W(37)); /* 38 */
  255. GG(c, d, e, a, b, W(38)); /* 39 */
  256. GG(b, c, d, e, a, W(39)); /* 40 */
  257. /* Round 3 */
  258. HH(a, b, c, d, e, W(40)); /* 41 */
  259. HH(e, a, b, c, d, W(41)); /* 42 */
  260. HH(d, e, a, b, c, W(42)); /* 43 */
  261. HH(c, d, e, a, b, W(43)); /* 44 */
  262. HH(b, c, d, e, a, W(44)); /* 45 */
  263. HH(a, b, c, d, e, W(45)); /* 46 */
  264. HH(e, a, b, c, d, W(46)); /* 47 */
  265. HH(d, e, a, b, c, W(47)); /* 48 */
  266. HH(c, d, e, a, b, W(48)); /* 49 */
  267. HH(b, c, d, e, a, W(49)); /* 50 */
  268. HH(a, b, c, d, e, W(50)); /* 51 */
  269. HH(e, a, b, c, d, W(51)); /* 52 */
  270. HH(d, e, a, b, c, W(52)); /* 53 */
  271. HH(c, d, e, a, b, W(53)); /* 54 */
  272. HH(b, c, d, e, a, W(54)); /* 55 */
  273. HH(a, b, c, d, e, W(55)); /* 56 */
  274. HH(e, a, b, c, d, W(56)); /* 57 */
  275. HH(d, e, a, b, c, W(57)); /* 58 */
  276. HH(c, d, e, a, b, W(58)); /* 59 */
  277. HH(b, c, d, e, a, W(59)); /* 60 */
  278. /* Round 4 */
  279. II(a, b, c, d, e, W(60)); /* 61 */
  280. II(e, a, b, c, d, W(61)); /* 62 */
  281. II(d, e, a, b, c, W(62)); /* 63 */
  282. II(c, d, e, a, b, W(63)); /* 64 */
  283. II(b, c, d, e, a, W(64)); /* 65 */
  284. II(a, b, c, d, e, W(65)); /* 66 */
  285. II(e, a, b, c, d, W(66)); /* 67 */
  286. II(d, e, a, b, c, W(67)); /* 68 */
  287. II(c, d, e, a, b, W(68)); /* 69 */
  288. II(b, c, d, e, a, W(69)); /* 70 */
  289. II(a, b, c, d, e, W(70)); /* 71 */
  290. II(e, a, b, c, d, W(71)); /* 72 */
  291. II(d, e, a, b, c, W(72)); /* 73 */
  292. II(c, d, e, a, b, W(73)); /* 74 */
  293. II(b, c, d, e, a, W(74)); /* 75 */
  294. II(a, b, c, d, e, W(75)); /* 76 */
  295. II(e, a, b, c, d, W(76)); /* 77 */
  296. II(d, e, a, b, c, W(77)); /* 78 */
  297. II(c, d, e, a, b, W(78)); /* 79 */
  298. II(b, c, d, e, a, W(79)); /* 80 */
  299. state[0] += a;
  300. state[1] += b;
  301. state[2] += c;
  302. state[3] += d;
  303. state[4] += e;
  304. /* Zeroize sensitive information. */
  305. ZEND_SECURE_ZERO((unsigned char*) x, sizeof(x));
  306. }
  307. /* }}} */
  308. /* {{{ SHA1Encode
  309. Encodes input (uint32_t) into output (unsigned char). Assumes len is
  310. a multiple of 4.
  311. */
  312. static void SHA1Encode(output, input, len)
  313. unsigned char *output;
  314. uint32_t *input;
  315. unsigned int len;
  316. {
  317. unsigned int i, j;
  318. for (i = 0, j = 0; j < len; i++, j += 4) {
  319. output[j] = (unsigned char) ((input[i] >> 24) & 0xff);
  320. output[j + 1] = (unsigned char) ((input[i] >> 16) & 0xff);
  321. output[j + 2] = (unsigned char) ((input[i] >> 8) & 0xff);
  322. output[j + 3] = (unsigned char) (input[i] & 0xff);
  323. }
  324. }
  325. /* }}} */
  326. /* {{{ SHA1Decode
  327. Decodes input (unsigned char) into output (uint32_t). Assumes len is
  328. a multiple of 4.
  329. */
  330. static void SHA1Decode(output, input, len)
  331. uint32_t *output;
  332. const unsigned char *input;
  333. unsigned int len;
  334. {
  335. unsigned int i, j;
  336. for (i = 0, j = 0; j < len; i++, j += 4)
  337. output[i] = ((uint32_t) input[j + 3]) | (((uint32_t) input[j + 2]) << 8) |
  338. (((uint32_t) input[j + 1]) << 16) | (((uint32_t) input[j]) << 24);
  339. }
  340. /* }}} */