PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/ext/hash/hash_md.c

http://github.com/php/php-src
C | 342 lines | 229 code | 48 blank | 65 comment | 14 complexity | 7a0ab8584d6be43c26bab1100074bd3d MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
  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. | http://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. | Taken from: ext/standard/md5.c |
  14. +----------------------------------------------------------------------+
  15. */
  16. #include "php_hash.h"
  17. #include "php_hash_md.h"
  18. const php_hash_ops php_hash_md5_ops = {
  19. (php_hash_init_func_t) PHP_MD5Init,
  20. (php_hash_update_func_t) PHP_MD5Update,
  21. (php_hash_final_func_t) PHP_MD5Final,
  22. (php_hash_copy_func_t) php_hash_copy,
  23. 16,
  24. 64,
  25. sizeof(PHP_MD5_CTX),
  26. 1
  27. };
  28. const php_hash_ops php_hash_md4_ops = {
  29. (php_hash_init_func_t) PHP_MD4Init,
  30. (php_hash_update_func_t) PHP_MD4Update,
  31. (php_hash_final_func_t) PHP_MD4Final,
  32. (php_hash_copy_func_t) php_hash_copy,
  33. 16,
  34. 64,
  35. sizeof(PHP_MD4_CTX),
  36. 1
  37. };
  38. const php_hash_ops php_hash_md2_ops = {
  39. (php_hash_init_func_t) PHP_MD2Init,
  40. (php_hash_update_func_t) PHP_MD2Update,
  41. (php_hash_final_func_t) PHP_MD2Final,
  42. (php_hash_copy_func_t) php_hash_copy,
  43. 16,
  44. 16,
  45. sizeof(PHP_MD2_CTX),
  46. 1
  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 (uint32_t) into output (unsigned char). Assumes len is
  57. a multiple of 4.
  58. */
  59. static void Encode(unsigned char *output, uint32_t *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 (uint32_t). Assumes len is
  72. a multiple of 4.
  73. */
  74. static void Decode(uint32_t *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] = ((uint32_t) input[j]) | (((uint32_t) input[j + 1]) << 8) |
  79. (((uint32_t) input[j + 2]) << 16) | (((uint32_t) input[j + 3]) << 24);
  80. }
  81. /* }}} */
  82. /* MD4 */
  83. #define MD4_F(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
  84. #define MD4_G(x,y,z) (((x) & ((y) | (z))) | ((y) & (z)))
  85. #define MD4_H(x,y,z) ((x) ^ (y) ^ (z))
  86. #define ROTL32(s,v) (((v) << (s)) | ((v) >> (32 - (s))))
  87. #define MD4_R1(a,b,c,d,k,s) a = ROTL32(s, a + MD4_F(b,c,d) + x[k])
  88. #define MD4_R2(a,b,c,d,k,s) a = ROTL32(s, a + MD4_G(b,c,d) + x[k] + 0x5A827999)
  89. #define MD4_R3(a,b,c,d,k,s) a = ROTL32(s, a + MD4_H(b,c,d) + x[k] + 0x6ED9EBA1)
  90. static void MD4Transform(uint32_t state[4], const unsigned char block[64])
  91. {
  92. uint32_t a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  93. Decode(x, block, 64);
  94. /* Round 1 */
  95. MD4_R1(a,b,c,d, 0, 3);
  96. MD4_R1(d,a,b,c, 1, 7);
  97. MD4_R1(c,d,a,b, 2,11);
  98. MD4_R1(b,c,d,a, 3,19);
  99. MD4_R1(a,b,c,d, 4, 3);
  100. MD4_R1(d,a,b,c, 5, 7);
  101. MD4_R1(c,d,a,b, 6,11);
  102. MD4_R1(b,c,d,a, 7,19);
  103. MD4_R1(a,b,c,d, 8, 3);
  104. MD4_R1(d,a,b,c, 9, 7);
  105. MD4_R1(c,d,a,b,10,11);
  106. MD4_R1(b,c,d,a,11,19);
  107. MD4_R1(a,b,c,d,12, 3);
  108. MD4_R1(d,a,b,c,13, 7);
  109. MD4_R1(c,d,a,b,14,11);
  110. MD4_R1(b,c,d,a,15,19);
  111. /* Round 2 */
  112. MD4_R2(a,b,c,d, 0, 3);
  113. MD4_R2(d,a,b,c, 4, 5);
  114. MD4_R2(c,d,a,b, 8, 9);
  115. MD4_R2(b,c,d,a,12,13);
  116. MD4_R2(a,b,c,d, 1, 3);
  117. MD4_R2(d,a,b,c, 5, 5);
  118. MD4_R2(c,d,a,b, 9, 9);
  119. MD4_R2(b,c,d,a,13,13);
  120. MD4_R2(a,b,c,d, 2, 3);
  121. MD4_R2(d,a,b,c, 6, 5);
  122. MD4_R2(c,d,a,b,10, 9);
  123. MD4_R2(b,c,d,a,14,13);
  124. MD4_R2(a,b,c,d, 3, 3);
  125. MD4_R2(d,a,b,c, 7, 5);
  126. MD4_R2(c,d,a,b,11, 9);
  127. MD4_R2(b,c,d,a,15,13);
  128. /* Round 3 */
  129. MD4_R3(a,b,c,d, 0, 3);
  130. MD4_R3(d,a,b,c, 8, 9);
  131. MD4_R3(c,d,a,b, 4,11);
  132. MD4_R3(b,c,d,a,12,15);
  133. MD4_R3(a,b,c,d, 2, 3);
  134. MD4_R3(d,a,b,c,10, 9);
  135. MD4_R3(c,d,a,b, 6,11);
  136. MD4_R3(b,c,d,a,14,15);
  137. MD4_R3(a,b,c,d, 1, 3);
  138. MD4_R3(d,a,b,c, 9, 9);
  139. MD4_R3(c,d,a,b, 5,11);
  140. MD4_R3(b,c,d,a,13,15);
  141. MD4_R3(a,b,c,d, 3, 3);
  142. MD4_R3(d,a,b,c,11, 9);
  143. MD4_R3(c,d,a,b, 7,11);
  144. MD4_R3(b,c,d,a,15,15);
  145. state[0] += a;
  146. state[1] += b;
  147. state[2] += c;
  148. state[3] += d;
  149. }
  150. /* {{{ PHP_MD4Init
  151. * MD4 initialization. Begins an MD4 operation, writing a new context.
  152. */
  153. PHP_HASH_API void PHP_MD4Init(PHP_MD4_CTX * context)
  154. {
  155. context->count[0] = context->count[1] = 0;
  156. /* Load magic initialization constants.
  157. */
  158. context->state[0] = 0x67452301;
  159. context->state[1] = 0xefcdab89;
  160. context->state[2] = 0x98badcfe;
  161. context->state[3] = 0x10325476;
  162. }
  163. /* }}} */
  164. /* {{{ PHP_MD4Update
  165. MD4 block update operation. Continues an MD4 message-digest
  166. operation, processing another message block, and updating the
  167. context.
  168. */
  169. PHP_HASH_API void PHP_MD4Update(PHP_MD4_CTX * context, const unsigned char *input, size_t inputLen)
  170. {
  171. unsigned int i, index, partLen;
  172. /* Compute number of bytes mod 64 */
  173. index = (unsigned int) ((context->count[0] >> 3) & 0x3F);
  174. /* Update number of bits */
  175. if ((context->count[0] += ((uint32_t) inputLen << 3))
  176. < ((uint32_t) inputLen << 3))
  177. context->count[1]++;
  178. context->count[1] += ((uint32_t) inputLen >> 29);
  179. partLen = 64 - index;
  180. /* Transform as many times as possible.
  181. */
  182. if (inputLen >= partLen) {
  183. memcpy((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
  184. MD4Transform(context->state, context->buffer);
  185. for (i = partLen; i + 63 < inputLen; i += 64) {
  186. MD4Transform(context->state, &input[i]);
  187. }
  188. index = 0;
  189. } else {
  190. i = 0;
  191. }
  192. /* Buffer remaining input */
  193. memcpy((unsigned char*) & context->buffer[index], (unsigned char*) & input[i], inputLen - i);
  194. }
  195. /* }}} */
  196. /* {{{ PHP_MD4Final
  197. MD4 finalization. Ends an MD4 message-digest operation, writing the
  198. the message digest and zeroizing the context.
  199. */
  200. PHP_HASH_API void PHP_MD4Final(unsigned char digest[16], PHP_MD4_CTX * context)
  201. {
  202. unsigned char bits[8];
  203. unsigned int index, padLen;
  204. /* Save number of bits */
  205. Encode(bits, context->count, 8);
  206. /* Pad out to 56 mod 64.
  207. */
  208. index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
  209. padLen = (index < 56) ? (56 - index) : (120 - index);
  210. PHP_MD4Update(context, PADDING, padLen);
  211. /* Append length (before padding) */
  212. PHP_MD4Update(context, bits, 8);
  213. /* Store state in digest */
  214. Encode(digest, context->state, 16);
  215. /* Zeroize sensitive information.
  216. */
  217. ZEND_SECURE_ZERO((unsigned char*) context, sizeof(*context));
  218. }
  219. /* }}} */
  220. /* MD2 */
  221. static const unsigned char MD2_S[256] = {
  222. 41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6, 19,
  223. 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188, 76, 130, 202,
  224. 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24, 138, 23, 229, 18,
  225. 190, 78, 196, 214, 218, 158, 222, 73, 160, 251, 245, 142, 187, 47, 238, 122,
  226. 169, 104, 121, 145, 21, 178, 7, 63, 148, 194, 16, 137, 11, 34, 95, 33,
  227. 128, 127, 93, 154, 90, 144, 50, 39, 53, 62, 204, 231, 191, 247, 151, 3,
  228. 255, 25, 48, 179, 72, 165, 181, 209, 215, 94, 146, 42, 172, 86, 170, 198,
  229. 79, 184, 56, 210, 150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241,
  230. 69, 157, 112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2,
  231. 27, 96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15,
  232. 85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197, 234, 38,
  233. 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65, 129, 77, 82,
  234. 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123, 8, 12, 189, 177, 74,
  235. 120, 136, 149, 139, 227, 99, 232, 109, 233, 203, 213, 254, 59, 0, 29, 57,
  236. 242, 239, 183, 14, 102, 88, 208, 228, 166, 119, 114, 248, 235, 117, 75, 10,
  237. 49, 68, 80, 180, 143, 237, 31, 26, 219, 153, 141, 51, 159, 17, 131, 20 };
  238. PHP_HASH_API void PHP_MD2Init(PHP_MD2_CTX *context)
  239. {
  240. memset(context, 0, sizeof(PHP_MD2_CTX));
  241. }
  242. static void MD2_Transform(PHP_MD2_CTX *context, const unsigned char *block)
  243. {
  244. unsigned char i,j,t = 0;
  245. for(i = 0; i < 16; i++) {
  246. context->state[16+i] = block[i];
  247. context->state[32+i] = (context->state[16+i] ^ context->state[i]);
  248. }
  249. for(i = 0; i < 18; i++) {
  250. for(j = 0; j < 48; j++) {
  251. t = context->state[j] = context->state[j] ^ MD2_S[t];
  252. }
  253. t += i;
  254. }
  255. /* Update checksum -- must be after transform to avoid fouling up last message block */
  256. t = context->checksum[15];
  257. for(i = 0; i < 16; i++) {
  258. t = context->checksum[i] ^= MD2_S[block[i] ^ t];
  259. }
  260. }
  261. PHP_HASH_API void PHP_MD2Update(PHP_MD2_CTX *context, const unsigned char *buf, size_t len)
  262. {
  263. const unsigned char *p = buf, *e = buf + len;
  264. if (context->in_buffer) {
  265. if (context->in_buffer + len < 16) {
  266. /* Not enough for block, just pass into buffer */
  267. memcpy(context->buffer + context->in_buffer, p, len);
  268. context->in_buffer += (char) len;
  269. return;
  270. }
  271. /* Put buffered data together with inbound for a single block */
  272. memcpy(context->buffer + context->in_buffer, p, 16 - context->in_buffer);
  273. MD2_Transform(context, context->buffer);
  274. p += 16 - context->in_buffer;
  275. context->in_buffer = 0;
  276. }
  277. /* Process as many whole blocks as remain */
  278. while ((p + 16) <= e) {
  279. MD2_Transform(context, p);
  280. p += 16;
  281. }
  282. /* Copy remaining data to buffer */
  283. if (p < e) {
  284. memcpy(context->buffer, p, e - p);
  285. context->in_buffer = (char) (e - p);
  286. }
  287. }
  288. PHP_HASH_API void PHP_MD2Final(unsigned char output[16], PHP_MD2_CTX *context)
  289. {
  290. memset(context->buffer + context->in_buffer, 16 - context->in_buffer, 16 - context->in_buffer);
  291. MD2_Transform(context, context->buffer);
  292. MD2_Transform(context, context->checksum);
  293. memcpy(output, context->state, 16);
  294. }