md5.c 11 KB

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