PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/ext/hash/hash_md.c

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