md5.c 11 KB

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