md5.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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: Alexander Peslyak (Solar Designer) <solar at openwall.com> |
  16. | Lachlan Roche |
  17. | Alessandro Astarita <aleast@capri.it> |
  18. +----------------------------------------------------------------------+
  19. */
  20. #include "php.h"
  21. #include "md5.h"
  22. PHPAPI void make_digest(char *md5str, const unsigned char *digest) /* {{{ */
  23. {
  24. make_digest_ex(md5str, digest, 16);
  25. }
  26. /* }}} */
  27. PHPAPI void make_digest_ex(char *md5str, const unsigned char *digest, int len) /* {{{ */
  28. {
  29. static const char hexits[17] = "0123456789abcdef";
  30. int i;
  31. for (i = 0; i < len; i++) {
  32. md5str[i * 2] = hexits[digest[i] >> 4];
  33. md5str[(i * 2) + 1] = hexits[digest[i] & 0x0F];
  34. }
  35. md5str[len * 2] = '\0';
  36. }
  37. /* }}} */
  38. /* {{{ proto string md5(string str, [ bool raw_output])
  39. Calculate the md5 hash of a string */
  40. PHP_NAMED_FUNCTION(php_if_md5)
  41. {
  42. zend_string *arg;
  43. zend_bool raw_output = 0;
  44. PHP_MD5_CTX context;
  45. unsigned char digest[16];
  46. ZEND_PARSE_PARAMETERS_START(1, 2)
  47. Z_PARAM_STR(arg)
  48. Z_PARAM_OPTIONAL
  49. Z_PARAM_BOOL(raw_output)
  50. ZEND_PARSE_PARAMETERS_END();
  51. PHP_MD5Init(&context);
  52. PHP_MD5Update(&context, ZSTR_VAL(arg), ZSTR_LEN(arg));
  53. PHP_MD5Final(digest, &context);
  54. if (raw_output) {
  55. RETURN_STRINGL((char *) digest, 16);
  56. } else {
  57. RETVAL_NEW_STR(zend_string_alloc(32, 0));
  58. make_digest_ex(Z_STRVAL_P(return_value), digest, 16);
  59. }
  60. }
  61. /* }}} */
  62. /* {{{ proto string md5_file(string filename [, bool raw_output])
  63. Calculate the md5 hash of given filename */
  64. PHP_NAMED_FUNCTION(php_if_md5_file)
  65. {
  66. char *arg;
  67. size_t arg_len;
  68. zend_bool raw_output = 0;
  69. unsigned char buf[1024];
  70. unsigned char digest[16];
  71. PHP_MD5_CTX context;
  72. size_t n;
  73. php_stream *stream;
  74. ZEND_PARSE_PARAMETERS_START(1, 2)
  75. Z_PARAM_PATH(arg, arg_len)
  76. Z_PARAM_OPTIONAL
  77. Z_PARAM_BOOL(raw_output)
  78. ZEND_PARSE_PARAMETERS_END();
  79. stream = php_stream_open_wrapper(arg, "rb", REPORT_ERRORS, NULL);
  80. if (!stream) {
  81. RETURN_FALSE;
  82. }
  83. PHP_MD5Init(&context);
  84. while ((n = php_stream_read(stream, (char*)buf, sizeof(buf))) > 0) {
  85. PHP_MD5Update(&context, buf, n);
  86. }
  87. /* XXX this probably can be improved with some number of retries */
  88. if (!php_stream_eof(stream)) {
  89. php_stream_close(stream);
  90. PHP_MD5Final(digest, &context);
  91. RETURN_FALSE;
  92. }
  93. php_stream_close(stream);
  94. PHP_MD5Final(digest, &context);
  95. if (raw_output) {
  96. RETURN_STRINGL((char *) digest, 16);
  97. } else {
  98. RETVAL_NEW_STR(zend_string_alloc(32, 0));
  99. make_digest_ex(Z_STRVAL_P(return_value), digest, 16);
  100. }
  101. }
  102. /* }}} */
  103. /*
  104. * This is an OpenSSL-compatible implementation of the RSA Data Security,
  105. * Inc. MD5 Message-Digest Algorithm (RFC 1321).
  106. *
  107. * Written by Solar Designer <solar at openwall.com> in 2001, and placed
  108. * in the public domain. There's absolutely no warranty.
  109. *
  110. * This differs from Colin Plumb's older public domain implementation in
  111. * that no 32-bit integer data type is required, there's no compile-time
  112. * endianness configuration, and the function prototypes match OpenSSL's.
  113. * The primary goals are portability and ease of use.
  114. *
  115. * This implementation is meant to be fast, but not as fast as possible.
  116. * Some known optimizations are not included to reduce source code size
  117. * and avoid compile-time configuration.
  118. */
  119. #include <string.h>
  120. /*
  121. * The basic MD5 functions.
  122. *
  123. * F and G are optimized compared to their RFC 1321 definitions for
  124. * architectures that lack an AND-NOT instruction, just like in Colin Plumb's
  125. * implementation.
  126. */
  127. #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
  128. #define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y))))
  129. #define H(x, y, z) ((x) ^ (y) ^ (z))
  130. #define I(x, y, z) ((y) ^ ((x) | ~(z)))
  131. /*
  132. * The MD5 transformation for all four rounds.
  133. */
  134. #define STEP(f, a, b, c, d, x, t, s) \
  135. (a) += f((b), (c), (d)) + (x) + (t); \
  136. (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \
  137. (a) += (b);
  138. /*
  139. * SET reads 4 input bytes in little-endian byte order and stores them
  140. * in a properly aligned word in host byte order.
  141. *
  142. * The check for little-endian architectures that tolerate unaligned
  143. * memory accesses is just an optimization. Nothing will break if it
  144. * doesn't work.
  145. */
  146. #if defined(__i386__) || defined(__x86_64__) || defined(__vax__)
  147. # define SET(n) \
  148. (*(uint32_t *)&ptr[(n) * 4])
  149. # define GET(n) \
  150. SET(n)
  151. #else
  152. # define SET(n) \
  153. (ctx->block[(n)] = \
  154. (uint32_t)ptr[(n) * 4] | \
  155. ((uint32_t)ptr[(n) * 4 + 1] << 8) | \
  156. ((uint32_t)ptr[(n) * 4 + 2] << 16) | \
  157. ((uint32_t)ptr[(n) * 4 + 3] << 24))
  158. # define GET(n) \
  159. (ctx->block[(n)])
  160. #endif
  161. /*
  162. * This processes one or more 64-byte data blocks, but does NOT update
  163. * the bit counters. There are no alignment requirements.
  164. */
  165. static const void *body(PHP_MD5_CTX *ctx, const void *data, size_t size)
  166. {
  167. const unsigned char *ptr;
  168. uint32_t a, b, c, d;
  169. uint32_t saved_a, saved_b, saved_c, saved_d;
  170. ptr = data;
  171. a = ctx->a;
  172. b = ctx->b;
  173. c = ctx->c;
  174. d = ctx->d;
  175. do {
  176. saved_a = a;
  177. saved_b = b;
  178. saved_c = c;
  179. saved_d = d;
  180. /* Round 1 */
  181. STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7)
  182. STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12)
  183. STEP(F, c, d, a, b, SET(2), 0x242070db, 17)
  184. STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22)
  185. STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7)
  186. STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12)
  187. STEP(F, c, d, a, b, SET(6), 0xa8304613, 17)
  188. STEP(F, b, c, d, a, SET(7), 0xfd469501, 22)
  189. STEP(F, a, b, c, d, SET(8), 0x698098d8, 7)
  190. STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12)
  191. STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17)
  192. STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22)
  193. STEP(F, a, b, c, d, SET(12), 0x6b901122, 7)
  194. STEP(F, d, a, b, c, SET(13), 0xfd987193, 12)
  195. STEP(F, c, d, a, b, SET(14), 0xa679438e, 17)
  196. STEP(F, b, c, d, a, SET(15), 0x49b40821, 22)
  197. /* Round 2 */
  198. STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5)
  199. STEP(G, d, a, b, c, GET(6), 0xc040b340, 9)
  200. STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14)
  201. STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20)
  202. STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5)
  203. STEP(G, d, a, b, c, GET(10), 0x02441453, 9)
  204. STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14)
  205. STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20)
  206. STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5)
  207. STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9)
  208. STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14)
  209. STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20)
  210. STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5)
  211. STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9)
  212. STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14)
  213. STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20)
  214. /* Round 3 */
  215. STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4)
  216. STEP(H, d, a, b, c, GET(8), 0x8771f681, 11)
  217. STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16)
  218. STEP(H, b, c, d, a, GET(14), 0xfde5380c, 23)
  219. STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4)
  220. STEP(H, d, a, b, c, GET(4), 0x4bdecfa9, 11)
  221. STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16)
  222. STEP(H, b, c, d, a, GET(10), 0xbebfbc70, 23)
  223. STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4)
  224. STEP(H, d, a, b, c, GET(0), 0xeaa127fa, 11)
  225. STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16)
  226. STEP(H, b, c, d, a, GET(6), 0x04881d05, 23)
  227. STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4)
  228. STEP(H, d, a, b, c, GET(12), 0xe6db99e5, 11)
  229. STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16)
  230. STEP(H, b, c, d, a, GET(2), 0xc4ac5665, 23)
  231. /* Round 4 */
  232. STEP(I, a, b, c, d, GET(0), 0xf4292244, 6)
  233. STEP(I, d, a, b, c, GET(7), 0x432aff97, 10)
  234. STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15)
  235. STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21)
  236. STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6)
  237. STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10)
  238. STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15)
  239. STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21)
  240. STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6)
  241. STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10)
  242. STEP(I, c, d, a, b, GET(6), 0xa3014314, 15)
  243. STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21)
  244. STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6)
  245. STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10)
  246. STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15)
  247. STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21)
  248. a += saved_a;
  249. b += saved_b;
  250. c += saved_c;
  251. d += saved_d;
  252. ptr += 64;
  253. } while (size -= 64);
  254. ctx->a = a;
  255. ctx->b = b;
  256. ctx->c = c;
  257. ctx->d = d;
  258. return ptr;
  259. }
  260. PHPAPI void PHP_MD5Init(PHP_MD5_CTX *ctx)
  261. {
  262. ctx->a = 0x67452301;
  263. ctx->b = 0xefcdab89;
  264. ctx->c = 0x98badcfe;
  265. ctx->d = 0x10325476;
  266. ctx->lo = 0;
  267. ctx->hi = 0;
  268. }
  269. PHPAPI void PHP_MD5Update(PHP_MD5_CTX *ctx, const void *data, size_t size)
  270. {
  271. uint32_t saved_lo;
  272. uint32_t used, free;
  273. saved_lo = ctx->lo;
  274. if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo) {
  275. ctx->hi++;
  276. }
  277. ctx->hi += size >> 29;
  278. used = saved_lo & 0x3f;
  279. if (used) {
  280. free = 64 - used;
  281. if (size < free) {
  282. memcpy(&ctx->buffer[used], data, size);
  283. return;
  284. }
  285. memcpy(&ctx->buffer[used], data, free);
  286. data = (unsigned char *)data + free;
  287. size -= free;
  288. body(ctx, ctx->buffer, 64);
  289. }
  290. if (size >= 64) {
  291. data = body(ctx, data, size & ~(size_t)0x3f);
  292. size &= 0x3f;
  293. }
  294. memcpy(ctx->buffer, data, size);
  295. }
  296. PHPAPI void PHP_MD5Final(unsigned char *result, PHP_MD5_CTX *ctx)
  297. {
  298. uint32_t used, free;
  299. used = ctx->lo & 0x3f;
  300. ctx->buffer[used++] = 0x80;
  301. free = 64 - used;
  302. if (free < 8) {
  303. memset(&ctx->buffer[used], 0, free);
  304. body(ctx, ctx->buffer, 64);
  305. used = 0;
  306. free = 64;
  307. }
  308. memset(&ctx->buffer[used], 0, free - 8);
  309. ctx->lo <<= 3;
  310. ctx->buffer[56] = ctx->lo;
  311. ctx->buffer[57] = ctx->lo >> 8;
  312. ctx->buffer[58] = ctx->lo >> 16;
  313. ctx->buffer[59] = ctx->lo >> 24;
  314. ctx->buffer[60] = ctx->hi;
  315. ctx->buffer[61] = ctx->hi >> 8;
  316. ctx->buffer[62] = ctx->hi >> 16;
  317. ctx->buffer[63] = ctx->hi >> 24;
  318. body(ctx, ctx->buffer, 64);
  319. result[0] = ctx->a;
  320. result[1] = ctx->a >> 8;
  321. result[2] = ctx->a >> 16;
  322. result[3] = ctx->a >> 24;
  323. result[4] = ctx->b;
  324. result[5] = ctx->b >> 8;
  325. result[6] = ctx->b >> 16;
  326. result[7] = ctx->b >> 24;
  327. result[8] = ctx->c;
  328. result[9] = ctx->c >> 8;
  329. result[10] = ctx->c >> 16;
  330. result[11] = ctx->c >> 24;
  331. result[12] = ctx->d;
  332. result[13] = ctx->d >> 8;
  333. result[14] = ctx->d >> 16;
  334. result[15] = ctx->d >> 24;
  335. ZEND_SECURE_ZERO(ctx, sizeof(*ctx));
  336. }
  337. /*
  338. * Local variables:
  339. * tab-width: 4
  340. * c-basic-offset: 4
  341. * End:
  342. * vim600: sw=4 ts=4 fdm=marker
  343. * vim<600: sw=4 ts=4
  344. */