hash_md.c 11 KB

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