hash_md.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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. | Taken from: ext/standard/md5.c |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include "php_hash.h"
  19. #include "php_hash_md.h"
  20. const php_hash_ops php_hash_md5_ops = {
  21. (php_hash_init_func_t) PHP_MD5Init,
  22. (php_hash_update_func_t) PHP_MD5Update,
  23. (php_hash_final_func_t) PHP_MD5Final,
  24. (php_hash_copy_func_t) php_hash_copy,
  25. 16,
  26. 64,
  27. sizeof(PHP_MD5_CTX),
  28. 1
  29. };
  30. const php_hash_ops php_hash_md4_ops = {
  31. (php_hash_init_func_t) PHP_MD4Init,
  32. (php_hash_update_func_t) PHP_MD4Update,
  33. (php_hash_final_func_t) PHP_MD4Final,
  34. (php_hash_copy_func_t) php_hash_copy,
  35. 16,
  36. 64,
  37. sizeof(PHP_MD4_CTX),
  38. 1
  39. };
  40. const php_hash_ops php_hash_md2_ops = {
  41. (php_hash_init_func_t) PHP_MD2Init,
  42. (php_hash_update_func_t) PHP_MD2Update,
  43. (php_hash_final_func_t) PHP_MD2Final,
  44. (php_hash_copy_func_t) php_hash_copy,
  45. 16,
  46. 16,
  47. sizeof(PHP_MD2_CTX),
  48. 1
  49. };
  50. /* MD common stuff */
  51. static const unsigned char PADDING[64] =
  52. {
  53. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  54. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  55. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  56. };
  57. /* {{{ Encode
  58. Encodes input (uint32_t) into output (unsigned char). Assumes len is
  59. a multiple of 4.
  60. */
  61. static void Encode(unsigned char *output, uint32_t *input, unsigned int len)
  62. {
  63. unsigned int i, j;
  64. for (i = 0, j = 0; j < len; i++, j += 4) {
  65. output[j] = (unsigned char) (input[i] & 0xff);
  66. output[j + 1] = (unsigned char) ((input[i] >> 8) & 0xff);
  67. output[j + 2] = (unsigned char) ((input[i] >> 16) & 0xff);
  68. output[j + 3] = (unsigned char) ((input[i] >> 24) & 0xff);
  69. }
  70. }
  71. /* }}} */
  72. /* {{{ Decode
  73. Decodes input (unsigned char) into output (uint32_t). Assumes len is
  74. a multiple of 4.
  75. */
  76. static void Decode(uint32_t *output, const unsigned char *input, unsigned int len)
  77. {
  78. unsigned int i, j;
  79. for (i = 0, j = 0; j < len; i++, j += 4)
  80. output[i] = ((uint32_t) input[j]) | (((uint32_t) input[j + 1]) << 8) |
  81. (((uint32_t) input[j + 2]) << 16) | (((uint32_t) input[j + 3]) << 24);
  82. }
  83. /* }}} */
  84. #ifdef PHP_HASH_MD5_NOT_IN_CORE
  85. /* MD5 */
  86. PHP_HASH_API void make_digest(char *md5str, unsigned char *digest)
  87. {
  88. php_hash_bin2hex(md5str, digest, 16);
  89. md5str[32] = '\0';
  90. }
  91. /* {{{ proto string md5(string str, [ bool raw_output])
  92. Calculate the md5 hash of a string */
  93. PHP_NAMED_FUNCTION(php_if_md5)
  94. {
  95. char *arg;
  96. size_t arg_len;
  97. zend_bool raw_output = 0;
  98. PHP_MD5_CTX context;
  99. unsigned char digest[16];
  100. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|b", &arg, &arg_len, &raw_output) == FAILURE) {
  101. return;
  102. }
  103. PHP_MD5Init(&context);
  104. PHP_MD5Update(&context, arg, arg_len);
  105. PHP_MD5Final(digest, &context);
  106. if (raw_output) {
  107. RETURN_STRINGL(digest, 16);
  108. } else {
  109. RETVAL_NEW_STR(zend_string_alloc(32, 0));
  110. make_digest(Z_STRVAL_P(return_value), digest);
  111. }
  112. }
  113. /* }}} */
  114. /* {{{ proto string md5_file(string filename [, bool raw_output])
  115. Calculate the md5 hash of given filename */
  116. PHP_NAMED_FUNCTION(php_if_md5_file)
  117. {
  118. char *arg;
  119. size_t arg_len;
  120. zend_bool raw_output = 0;
  121. unsigned char buf[1024];
  122. unsigned char digest[16];
  123. PHP_MD5_CTX context;
  124. int n;
  125. php_stream *stream;
  126. if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|b", &arg, &arg_len, &raw_output) == FAILURE) {
  127. return;
  128. }
  129. stream = php_stream_open_wrapper(arg, "rb", REPORT_ERRORS, NULL);
  130. if (!stream) {
  131. RETURN_FALSE;
  132. }
  133. PHP_MD5Init(&context);
  134. while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
  135. PHP_MD5Update(&context, buf, n);
  136. }
  137. PHP_MD5Final(digest, &context);
  138. php_stream_close(stream);
  139. if (n<0) {
  140. RETURN_FALSE;
  141. }
  142. if (raw_output) {
  143. RETURN_STRINGL(digest, 16);
  144. } else {
  145. RETVAL_NEW_STR(zend_string_alloc(32, 0));
  146. make_digest(Z_STRVAL_P(return_value), digest);
  147. }
  148. }
  149. /* }}} */
  150. /*
  151. * The remaining code is the reference MD5 code (md5c.c) from rfc1321
  152. */
  153. /* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
  154. */
  155. /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  156. rights reserved.
  157. License to copy and use this software is granted provided that it
  158. is identified as the "RSA Data Security, Inc. MD5 Message-Digest
  159. Algorithm" in all material mentioning or referencing this software
  160. or this function.
  161. License is also granted to make and use derivative works provided
  162. that such works are identified as "derived from the RSA Data
  163. Security, Inc. MD5 Message-Digest Algorithm" in all material
  164. mentioning or referencing the derived work.
  165. RSA Data Security, Inc. makes no representations concerning either
  166. the merchantability of this software or the suitability of this
  167. software for any particular purpose. It is provided "as is"
  168. without express or implied warranty of any kind.
  169. These notices must be retained in any copies of any part of this
  170. documentation and/or software.
  171. */
  172. /* Constants for MD5Transform routine.
  173. */
  174. #define S11 7
  175. #define S12 12
  176. #define S13 17
  177. #define S14 22
  178. #define S21 5
  179. #define S22 9
  180. #define S23 14
  181. #define S24 20
  182. #define S31 4
  183. #define S32 11
  184. #define S33 16
  185. #define S34 23
  186. #define S41 6
  187. #define S42 10
  188. #define S43 15
  189. #define S44 21
  190. static void MD5Transform(uint32_t[4], const unsigned char[64]);
  191. /* F, G, H and I are basic MD5 functions.
  192. */
  193. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  194. #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  195. #define H(x, y, z) ((x) ^ (y) ^ (z))
  196. #define I(x, y, z) ((y) ^ ((x) | (~z)))
  197. /* ROTATE_LEFT rotates x left n bits.
  198. */
  199. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  200. /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  201. Rotation is separate from addition to prevent recomputation.
  202. */
  203. #define FF(a, b, c, d, x, s, ac) { \
  204. (a) += F ((b), (c), (d)) + (x) + (uint32_t)(ac); \
  205. (a) = ROTATE_LEFT ((a), (s)); \
  206. (a) += (b); \
  207. }
  208. #define GG(a, b, c, d, x, s, ac) { \
  209. (a) += G ((b), (c), (d)) + (x) + (uint32_t)(ac); \
  210. (a) = ROTATE_LEFT ((a), (s)); \
  211. (a) += (b); \
  212. }
  213. #define HH(a, b, c, d, x, s, ac) { \
  214. (a) += H ((b), (c), (d)) + (x) + (uint32_t)(ac); \
  215. (a) = ROTATE_LEFT ((a), (s)); \
  216. (a) += (b); \
  217. }
  218. #define II(a, b, c, d, x, s, ac) { \
  219. (a) += I ((b), (c), (d)) + (x) + (uint32_t)(ac); \
  220. (a) = ROTATE_LEFT ((a), (s)); \
  221. (a) += (b); \
  222. }
  223. /* {{{ PHP_MD5Init
  224. * MD5 initialization. Begins an MD5 operation, writing a new context.
  225. */
  226. PHP_HASH_API void PHP_MD5Init(PHP_MD5_CTX * context)
  227. {
  228. context->count[0] = context->count[1] = 0;
  229. /* Load magic initialization constants.
  230. */
  231. context->state[0] = 0x67452301;
  232. context->state[1] = 0xefcdab89;
  233. context->state[2] = 0x98badcfe;
  234. context->state[3] = 0x10325476;
  235. }
  236. /* }}} */
  237. /* {{{ PHP_MD5Update
  238. MD5 block update operation. Continues an MD5 message-digest
  239. operation, processing another message block, and updating the
  240. context.
  241. */
  242. PHP_HASH_API void PHP_MD5Update(PHP_MD5_CTX * context, const unsigned char *input,
  243. unsigned int inputLen)
  244. {
  245. unsigned int i, index, partLen;
  246. /* Compute number of bytes mod 64 */
  247. index = (unsigned int) ((context->count[0] >> 3) & 0x3F);
  248. /* Update number of bits */
  249. if ((context->count[0] += ((uint32_t) inputLen << 3))
  250. < ((uint32_t) inputLen << 3))
  251. context->count[1]++;
  252. context->count[1] += ((uint32_t) inputLen >> 29);
  253. partLen = 64 - index;
  254. /* Transform as many times as possible.
  255. */
  256. if (inputLen >= partLen) {
  257. memcpy
  258. ((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
  259. MD5Transform(context->state, context->buffer);
  260. for (i = partLen; i + 63 < inputLen; i += 64)
  261. MD5Transform(context->state, &input[i]);
  262. index = 0;
  263. } else
  264. i = 0;
  265. /* Buffer remaining input */
  266. memcpy
  267. ((unsigned char*) & context->buffer[index], (unsigned char*) & input[i],
  268. inputLen - i);
  269. }
  270. /* }}} */
  271. /* {{{ PHP_MD5Final
  272. MD5 finalization. Ends an MD5 message-digest operation, writing the
  273. the message digest and zeroizing the context.
  274. */
  275. PHP_HASH_API void PHP_MD5Final(unsigned char digest[16], PHP_MD5_CTX * context)
  276. {
  277. unsigned char bits[8];
  278. unsigned int index, padLen;
  279. /* Save number of bits */
  280. Encode(bits, context->count, 8);
  281. /* Pad out to 56 mod 64.
  282. */
  283. index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
  284. padLen = (index < 56) ? (56 - index) : (120 - index);
  285. PHP_MD5Update(context, PADDING, padLen);
  286. /* Append length (before padding) */
  287. PHP_MD5Update(context, bits, 8);
  288. /* Store state in digest */
  289. Encode(digest, context->state, 16);
  290. /* Zeroize sensitive information.
  291. */
  292. ZEND_SECURE_ZERO((unsigned char*) context, sizeof(*context));
  293. }
  294. /* }}} */
  295. /* {{{ MD5Transform
  296. * MD5 basic transformation. Transforms state based on block.
  297. */
  298. static void MD5Transform(state, block)
  299. uint32_t state[4];
  300. const unsigned char block[64];
  301. {
  302. uint32_t a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  303. Decode(x, block, 64);
  304. /* Round 1 */
  305. FF(a, b, c, d, x[0], S11, 0xd76aa478); /* 1 */
  306. FF(d, a, b, c, x[1], S12, 0xe8c7b756); /* 2 */
  307. FF(c, d, a, b, x[2], S13, 0x242070db); /* 3 */
  308. FF(b, c, d, a, x[3], S14, 0xc1bdceee); /* 4 */
  309. FF(a, b, c, d, x[4], S11, 0xf57c0faf); /* 5 */
  310. FF(d, a, b, c, x[5], S12, 0x4787c62a); /* 6 */
  311. FF(c, d, a, b, x[6], S13, 0xa8304613); /* 7 */
  312. FF(b, c, d, a, x[7], S14, 0xfd469501); /* 8 */
  313. FF(a, b, c, d, x[8], S11, 0x698098d8); /* 9 */
  314. FF(d, a, b, c, x[9], S12, 0x8b44f7af); /* 10 */
  315. FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  316. FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  317. FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  318. FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  319. FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  320. FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
  321. /* Round 2 */
  322. GG(a, b, c, d, x[1], S21, 0xf61e2562); /* 17 */
  323. GG(d, a, b, c, x[6], S22, 0xc040b340); /* 18 */
  324. GG(c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  325. GG(b, c, d, a, x[0], S24, 0xe9b6c7aa); /* 20 */
  326. GG(a, b, c, d, x[5], S21, 0xd62f105d); /* 21 */
  327. GG(d, a, b, c, x[10], S22, 0x2441453); /* 22 */
  328. GG(c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  329. GG(b, c, d, a, x[4], S24, 0xe7d3fbc8); /* 24 */
  330. GG(a, b, c, d, x[9], S21, 0x21e1cde6); /* 25 */
  331. GG(d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  332. GG(c, d, a, b, x[3], S23, 0xf4d50d87); /* 27 */
  333. GG(b, c, d, a, x[8], S24, 0x455a14ed); /* 28 */
  334. GG(a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
  335. GG(d, a, b, c, x[2], S22, 0xfcefa3f8); /* 30 */
  336. GG(c, d, a, b, x[7], S23, 0x676f02d9); /* 31 */
  337. GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
  338. /* Round 3 */
  339. HH(a, b, c, d, x[5], S31, 0xfffa3942); /* 33 */
  340. HH(d, a, b, c, x[8], S32, 0x8771f681); /* 34 */
  341. HH(c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  342. HH(b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  343. HH(a, b, c, d, x[1], S31, 0xa4beea44); /* 37 */
  344. HH(d, a, b, c, x[4], S32, 0x4bdecfa9); /* 38 */
  345. HH(c, d, a, b, x[7], S33, 0xf6bb4b60); /* 39 */
  346. HH(b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  347. HH(a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  348. HH(d, a, b, c, x[0], S32, 0xeaa127fa); /* 42 */
  349. HH(c, d, a, b, x[3], S33, 0xd4ef3085); /* 43 */
  350. HH(b, c, d, a, x[6], S34, 0x4881d05); /* 44 */
  351. HH(a, b, c, d, x[9], S31, 0xd9d4d039); /* 45 */
  352. HH(d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  353. HH(c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  354. HH(b, c, d, a, x[2], S34, 0xc4ac5665); /* 48 */
  355. /* Round 4 */
  356. II(a, b, c, d, x[0], S41, 0xf4292244); /* 49 */
  357. II(d, a, b, c, x[7], S42, 0x432aff97); /* 50 */
  358. II(c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  359. II(b, c, d, a, x[5], S44, 0xfc93a039); /* 52 */
  360. II(a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  361. II(d, a, b, c, x[3], S42, 0x8f0ccc92); /* 54 */
  362. II(c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  363. II(b, c, d, a, x[1], S44, 0x85845dd1); /* 56 */
  364. II(a, b, c, d, x[8], S41, 0x6fa87e4f); /* 57 */
  365. II(d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  366. II(c, d, a, b, x[6], S43, 0xa3014314); /* 59 */
  367. II(b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  368. II(a, b, c, d, x[4], S41, 0xf7537e82); /* 61 */
  369. II(d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  370. II(c, d, a, b, x[2], S43, 0x2ad7d2bb); /* 63 */
  371. II(b, c, d, a, x[9], S44, 0xeb86d391); /* 64 */
  372. state[0] += a;
  373. state[1] += b;
  374. state[2] += c;
  375. state[3] += d;
  376. /* Zeroize sensitive information. */
  377. ZEND_SECURE_ZERO((unsigned char*) x, sizeof(x));
  378. }
  379. /* }}} */
  380. #endif /* PHP_HASH_MD5_NOT_IN_CORE */
  381. /* MD4 */
  382. #define MD4_F(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
  383. #define MD4_G(x,y,z) (((x) & ((y) | (z))) | ((y) & (z)))
  384. #define MD4_H(x,y,z) ((x) ^ (y) ^ (z))
  385. #define ROTL32(s,v) (((v) << (s)) | ((v) >> (32 - (s))))
  386. #define MD4_R1(a,b,c,d,k,s) a = ROTL32(s, a + MD4_F(b,c,d) + x[k])
  387. #define MD4_R2(a,b,c,d,k,s) a = ROTL32(s, a + MD4_G(b,c,d) + x[k] + 0x5A827999)
  388. #define MD4_R3(a,b,c,d,k,s) a = ROTL32(s, a + MD4_H(b,c,d) + x[k] + 0x6ED9EBA1)
  389. static void MD4Transform(uint32_t state[4], const unsigned char block[64])
  390. {
  391. uint32_t a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  392. Decode(x, block, 64);
  393. /* Round 1 */
  394. MD4_R1(a,b,c,d, 0, 3);
  395. MD4_R1(d,a,b,c, 1, 7);
  396. MD4_R1(c,d,a,b, 2,11);
  397. MD4_R1(b,c,d,a, 3,19);
  398. MD4_R1(a,b,c,d, 4, 3);
  399. MD4_R1(d,a,b,c, 5, 7);
  400. MD4_R1(c,d,a,b, 6,11);
  401. MD4_R1(b,c,d,a, 7,19);
  402. MD4_R1(a,b,c,d, 8, 3);
  403. MD4_R1(d,a,b,c, 9, 7);
  404. MD4_R1(c,d,a,b,10,11);
  405. MD4_R1(b,c,d,a,11,19);
  406. MD4_R1(a,b,c,d,12, 3);
  407. MD4_R1(d,a,b,c,13, 7);
  408. MD4_R1(c,d,a,b,14,11);
  409. MD4_R1(b,c,d,a,15,19);
  410. /* Round 2 */
  411. MD4_R2(a,b,c,d, 0, 3);
  412. MD4_R2(d,a,b,c, 4, 5);
  413. MD4_R2(c,d,a,b, 8, 9);
  414. MD4_R2(b,c,d,a,12,13);
  415. MD4_R2(a,b,c,d, 1, 3);
  416. MD4_R2(d,a,b,c, 5, 5);
  417. MD4_R2(c,d,a,b, 9, 9);
  418. MD4_R2(b,c,d,a,13,13);
  419. MD4_R2(a,b,c,d, 2, 3);
  420. MD4_R2(d,a,b,c, 6, 5);
  421. MD4_R2(c,d,a,b,10, 9);
  422. MD4_R2(b,c,d,a,14,13);
  423. MD4_R2(a,b,c,d, 3, 3);
  424. MD4_R2(d,a,b,c, 7, 5);
  425. MD4_R2(c,d,a,b,11, 9);
  426. MD4_R2(b,c,d,a,15,13);
  427. /* Round 3 */
  428. MD4_R3(a,b,c,d, 0, 3);
  429. MD4_R3(d,a,b,c, 8, 9);
  430. MD4_R3(c,d,a,b, 4,11);
  431. MD4_R3(b,c,d,a,12,15);
  432. MD4_R3(a,b,c,d, 2, 3);
  433. MD4_R3(d,a,b,c,10, 9);
  434. MD4_R3(c,d,a,b, 6,11);
  435. MD4_R3(b,c,d,a,14,15);
  436. MD4_R3(a,b,c,d, 1, 3);
  437. MD4_R3(d,a,b,c, 9, 9);
  438. MD4_R3(c,d,a,b, 5,11);
  439. MD4_R3(b,c,d,a,13,15);
  440. MD4_R3(a,b,c,d, 3, 3);
  441. MD4_R3(d,a,b,c,11, 9);
  442. MD4_R3(c,d,a,b, 7,11);
  443. MD4_R3(b,c,d,a,15,15);
  444. state[0] += a;
  445. state[1] += b;
  446. state[2] += c;
  447. state[3] += d;
  448. }
  449. /* {{{ PHP_MD4Init
  450. * MD4 initialization. Begins an MD4 operation, writing a new context.
  451. */
  452. PHP_HASH_API void PHP_MD4Init(PHP_MD4_CTX * context)
  453. {
  454. context->count[0] = context->count[1] = 0;
  455. /* Load magic initialization constants.
  456. */
  457. context->state[0] = 0x67452301;
  458. context->state[1] = 0xefcdab89;
  459. context->state[2] = 0x98badcfe;
  460. context->state[3] = 0x10325476;
  461. }
  462. /* }}} */
  463. /* {{{ PHP_MD4Update
  464. MD4 block update operation. Continues an MD4 message-digest
  465. operation, processing another message block, and updating the
  466. context.
  467. */
  468. PHP_HASH_API void PHP_MD4Update(PHP_MD4_CTX * context, const unsigned char *input, unsigned int inputLen)
  469. {
  470. unsigned int i, index, partLen;
  471. /* Compute number of bytes mod 64 */
  472. index = (unsigned int) ((context->count[0] >> 3) & 0x3F);
  473. /* Update number of bits */
  474. if ((context->count[0] += ((uint32_t) inputLen << 3))
  475. < ((uint32_t) inputLen << 3))
  476. context->count[1]++;
  477. context->count[1] += ((uint32_t) inputLen >> 29);
  478. partLen = 64 - index;
  479. /* Transform as many times as possible.
  480. */
  481. if (inputLen >= partLen) {
  482. memcpy((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
  483. MD4Transform(context->state, context->buffer);
  484. for (i = partLen; i + 63 < inputLen; i += 64) {
  485. MD4Transform(context->state, &input[i]);
  486. }
  487. index = 0;
  488. } else {
  489. i = 0;
  490. }
  491. /* Buffer remaining input */
  492. memcpy((unsigned char*) & context->buffer[index], (unsigned char*) & input[i], inputLen - i);
  493. }
  494. /* }}} */
  495. /* {{{ PHP_MD4Final
  496. MD4 finalization. Ends an MD4 message-digest operation, writing the
  497. the message digest and zeroizing the context.
  498. */
  499. PHP_HASH_API void PHP_MD4Final(unsigned char digest[16], PHP_MD4_CTX * context)
  500. {
  501. unsigned char bits[8];
  502. unsigned int index, padLen;
  503. /* Save number of bits */
  504. Encode(bits, context->count, 8);
  505. /* Pad out to 56 mod 64.
  506. */
  507. index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
  508. padLen = (index < 56) ? (56 - index) : (120 - index);
  509. PHP_MD4Update(context, PADDING, padLen);
  510. /* Append length (before padding) */
  511. PHP_MD4Update(context, bits, 8);
  512. /* Store state in digest */
  513. Encode(digest, context->state, 16);
  514. /* Zeroize sensitive information.
  515. */
  516. ZEND_SECURE_ZERO((unsigned char*) context, sizeof(*context));
  517. }
  518. /* }}} */
  519. /* MD2 */
  520. static const unsigned char MD2_S[256] = {
  521. 41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6, 19,
  522. 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188, 76, 130, 202,
  523. 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24, 138, 23, 229, 18,
  524. 190, 78, 196, 214, 218, 158, 222, 73, 160, 251, 245, 142, 187, 47, 238, 122,
  525. 169, 104, 121, 145, 21, 178, 7, 63, 148, 194, 16, 137, 11, 34, 95, 33,
  526. 128, 127, 93, 154, 90, 144, 50, 39, 53, 62, 204, 231, 191, 247, 151, 3,
  527. 255, 25, 48, 179, 72, 165, 181, 209, 215, 94, 146, 42, 172, 86, 170, 198,
  528. 79, 184, 56, 210, 150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241,
  529. 69, 157, 112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2,
  530. 27, 96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15,
  531. 85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197, 234, 38,
  532. 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65, 129, 77, 82,
  533. 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123, 8, 12, 189, 177, 74,
  534. 120, 136, 149, 139, 227, 99, 232, 109, 233, 203, 213, 254, 59, 0, 29, 57,
  535. 242, 239, 183, 14, 102, 88, 208, 228, 166, 119, 114, 248, 235, 117, 75, 10,
  536. 49, 68, 80, 180, 143, 237, 31, 26, 219, 153, 141, 51, 159, 17, 131, 20 };
  537. PHP_HASH_API void PHP_MD2Init(PHP_MD2_CTX *context)
  538. {
  539. memset(context, 0, sizeof(PHP_MD2_CTX));
  540. }
  541. static void MD2_Transform(PHP_MD2_CTX *context, const unsigned char *block)
  542. {
  543. unsigned char i,j,t = 0;
  544. for(i = 0; i < 16; i++) {
  545. context->state[16+i] = block[i];
  546. context->state[32+i] = (context->state[16+i] ^ context->state[i]);
  547. }
  548. for(i = 0; i < 18; i++) {
  549. for(j = 0; j < 48; j++) {
  550. t = context->state[j] = context->state[j] ^ MD2_S[t];
  551. }
  552. t += i;
  553. }
  554. /* Update checksum -- must be after transform to avoid fouling up last message block */
  555. t = context->checksum[15];
  556. for(i = 0; i < 16; i++) {
  557. t = context->checksum[i] ^= MD2_S[block[i] ^ t];
  558. }
  559. }
  560. PHP_HASH_API void PHP_MD2Update(PHP_MD2_CTX *context, const unsigned char *buf, unsigned int len)
  561. {
  562. const unsigned char *p = buf, *e = buf + len;
  563. if (context->in_buffer) {
  564. if (context->in_buffer + len < 16) {
  565. /* Not enough for block, just pass into buffer */
  566. memcpy(context->buffer + context->in_buffer, p, len);
  567. context->in_buffer += len;
  568. return;
  569. }
  570. /* Put buffered data together with inbound for a single block */
  571. memcpy(context->buffer + context->in_buffer, p, 16 - context->in_buffer);
  572. MD2_Transform(context, context->buffer);
  573. p += 16 - context->in_buffer;
  574. context->in_buffer = 0;
  575. }
  576. /* Process as many whole blocks as remain */
  577. while ((p + 16) <= e) {
  578. MD2_Transform(context, p);
  579. p += 16;
  580. }
  581. /* Copy remaining data to buffer */
  582. if (p < e) {
  583. memcpy(context->buffer, p, e - p);
  584. context->in_buffer = e - p;
  585. }
  586. }
  587. PHP_HASH_API void PHP_MD2Final(unsigned char output[16], PHP_MD2_CTX *context)
  588. {
  589. memset(context->buffer + context->in_buffer, 16 - context->in_buffer, 16 - context->in_buffer);
  590. MD2_Transform(context, context->buffer);
  591. MD2_Transform(context, context->checksum);
  592. memcpy(output, context->state, 16);
  593. }
  594. /*
  595. * Local variables:
  596. * tab-width: 4
  597. * c-basic-offset: 4
  598. * End:
  599. * vim600: sw=4 ts=4 fdm=marker
  600. * vim<600: sw=4 ts=4
  601. */