fork download
  1. /* Domain Cached Credentials 2 (MSCash2) example
  2.  * written by S3nf <thes3nf at googlemail.com> in 2010
  3.  * a slow but working implementation
  4.  *
  5.  * Generating Domain Cached Credentials for modern Windows operating systems, supporting:
  6.  * - Windows Vista
  7.  * - Windows 7
  8.  * - Windows Server 2008
  9.  *
  10.  * This software is based on:
  11.  * - the MSCASH patch for john written by Alain Espinosa <alainesp at gmail.com> in 2007
  12.  * - RFC 1320 - The MD4 Message-Digest Algorithm
  13.  * - RFC 2104 - HMAC: Keyed-Hashing for Message Authentication
  14.  * - RFC 3174 - US Secure Hash Algorithm 1 (SHA1)
  15.  * - the HMAC-SHA1 implementation of the PolarSSL open source cryptagraphic library (http://p...content-available-to-author-only...l.org/)
  16.  *
  17.  * This software was written by S3nf in 2010. No copyright is claimed, and the software is hereby placed in
  18.  * the public domain. In case this attempt to disclaim copyright and place the software in the public domain
  19.  * is deemed null and void, then the software is Copyright (c) 2010 S3nf and it is hereby released to the
  20.  * general public under the following terms:
  21.  *
  22.  * Redistribution and use in source and binary forms, with or without modification, are permitted.
  23.  *
  24.  */
  25.  
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29.  
  30. #define ITERATIONS 10240
  31.  
  32. #define INIT_MD4_A 0x67452301
  33. #define INIT_MD4_B 0xefcdab89
  34. #define INIT_MD4_C 0x98badcfe
  35. #define INIT_MD4_D 0x10325476
  36.  
  37. #define SQRT_2 0x5a827999
  38. #define SQRT_3 0x6ed9eba1
  39.  
  40. #define SHA1_DIGEST_LENGTH 20
  41.  
  42. #define INIT_SHA1_A 0x67452301
  43. #define INIT_SHA1_B 0xEFCDAB89
  44. #define INIT_SHA1_C 0x98BADCFE
  45. #define INIT_SHA1_D 0x10325476
  46. #define INIT_SHA1_E 0xC3D2E1F0
  47.  
  48. #ifndef GET_WORD_32_BE
  49. #define GET_WORD_32_BE(n,b,i) \
  50. { \
  51.   (n) = ( (unsigned long) (b)[(i) ] << 24 ) \
  52.   | ( (unsigned long) (b)[(i) + 1] << 16 ) \
  53.   | ( (unsigned long) (b)[(i) + 2] << 8 ) \
  54.   | ( (unsigned long) (b)[(i) + 3] ); \
  55. }
  56. #endif
  57.  
  58. #ifndef PUT_WORD_32_BE
  59. #define PUT_WORD_32_BE(n,b,i) \
  60. { \
  61.   (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  62.   (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  63.   (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  64.   (b)[(i) + 3] = (unsigned char) ( (n) ); \
  65. }
  66. #endif
  67.  
  68. #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
  69.  
  70. #define R(t) \
  71. ( \
  72.   temp = W[(t - 3) & 0x0F] ^ W[(t - 8) & 0x0F] ^ \
  73.   W[(t - 14) & 0x0F] ^ W[ t & 0x0F], \
  74.   ( W[t & 0x0F] = S(temp,1) ) \
  75. )
  76.  
  77. #define P(a,b,c,d,e,x) \
  78. { \
  79.   e += S(a,5) + F(b,c,d) + K + x; b = S(b,30); \
  80. }
  81.  
  82.  
  83. /*
  84.  * byte2hexstring
  85.  * convert byte array to hex string
  86.  */
  87. unsigned char *byte2hexstring(unsigned char * byte, unsigned int len) {
  88. unsigned int i;
  89. unsigned char *hexstring;
  90.  
  91. hexstring = malloc(len * 2 + 1);
  92. bzero(hexstring, 2 * len + 1);
  93.  
  94. for (i = 0; i < len; i++)
  95. sprintf(&hexstring[2 * i], "%02x", byte[i]);
  96.  
  97. return hexstring;
  98. }
  99.  
  100.  
  101. /*
  102.  * hmac_sha1
  103.  * based on RFC 2104, RFC 3174 and the HMAC-SHA1 implementation of the PolarSSL
  104.  * open source cryptographic library (http://w...content-available-to-author-only...l.org)
  105.  */
  106. static void hmac_sha1(const unsigned char *key, unsigned int keylen, const unsigned char *input, unsigned int inputlen, unsigned char *output)
  107. {
  108. unsigned int i, temp, W[16];
  109. unsigned int A, B, C, D, E, state[5];
  110. unsigned char buf[64];
  111. unsigned char ipad[64];
  112. unsigned char opad[64];
  113.  
  114. memset(ipad, 0x36, 64);
  115. memset(opad, 0x5C, 64);
  116. memset(buf, 0, 64);
  117.  
  118. // step 1: append zeros to the end of K to create a B Byte string
  119. memcpy(buf, input, inputlen);
  120. buf[inputlen] = 0x80;
  121. PUT_WORD_32_BE((64 + inputlen) << 3, buf, 60);
  122.  
  123. // step 2: XOR (bitwise exclusive-OR) the B byte string computed in step 1 with ipad
  124. // step 5: XOR (bitwise exclusive-OR) the B byte string computed in step 1 with opad
  125. for(i = 0; i < keylen; i++)
  126. {
  127. ipad[i] = ipad[i] ^ key[i];
  128. opad[i] = opad[i] ^ key[i];
  129. }
  130.  
  131. // step 3: append the stream of data 'text' to the B byte sting resulting from step 2
  132. // first part of stream (64 bytes) is ipad, second part of stream (64 bytes) is buf
  133.  
  134. // step 4: apply H to the stream (ipad & buf) generated in step 3
  135. GET_WORD_32_BE(W[ 0], ipad, 0);
  136. GET_WORD_32_BE(W[ 1], ipad, 4);
  137. GET_WORD_32_BE(W[ 2], ipad, 8);
  138. GET_WORD_32_BE(W[ 3], ipad, 12);
  139. GET_WORD_32_BE(W[ 4], ipad, 16);
  140. GET_WORD_32_BE(W[ 5], ipad, 20);
  141. GET_WORD_32_BE(W[ 6], ipad, 24);
  142. GET_WORD_32_BE(W[ 7], ipad, 28);
  143. GET_WORD_32_BE(W[ 8], ipad, 32);
  144. GET_WORD_32_BE(W[ 9], ipad, 36);
  145. GET_WORD_32_BE(W[10], ipad, 40);
  146. GET_WORD_32_BE(W[11], ipad, 44);
  147. GET_WORD_32_BE(W[12], ipad, 48);
  148. GET_WORD_32_BE(W[13], ipad, 52);
  149. GET_WORD_32_BE(W[14], ipad, 56);
  150. GET_WORD_32_BE(W[15], ipad, 60);
  151.  
  152. A = INIT_SHA1_A;
  153. B = INIT_SHA1_B;
  154. C = INIT_SHA1_C;
  155. D = INIT_SHA1_D;
  156. E = INIT_SHA1_E;
  157.  
  158. #define F(x,y,z) (z ^ (x & (y ^ z)))
  159. #define K 0x5A827999
  160.  
  161. P(A, B, C, D, E, W[0] );
  162. P(E, A, B, C, D, W[1] );
  163. P(D, E, A, B, C, W[2] );
  164. P(C, D, E, A, B, W[3] );
  165. P(B, C, D, E, A, W[4] );
  166. P(A, B, C, D, E, W[5] );
  167. P(E, A, B, C, D, W[6] );
  168. P(D, E, A, B, C, W[7] );
  169. P(C, D, E, A, B, W[8] );
  170. P(B, C, D, E, A, W[9] );
  171. P(A, B, C, D, E, W[10]);
  172. P(E, A, B, C, D, W[11]);
  173. P(D, E, A, B, C, W[12]);
  174. P(C, D, E, A, B, W[13]);
  175. P(B, C, D, E, A, W[14]);
  176. P(A, B, C, D, E, W[15]);
  177. P(E, A, B, C, D, R(16));
  178. P(D, E, A, B, C, R(17));
  179. P(C, D, E, A, B, R(18));
  180. P(B, C, D, E, A, R(19));
  181.  
  182. #undef K
  183. #undef F
  184.  
  185. #define F(x,y,z) (x ^ y ^ z)
  186. #define K 0x6ED9EBA1
  187.  
  188. P(A, B, C, D, E, R(20));
  189. P(E, A, B, C, D, R(21));
  190. P(D, E, A, B, C, R(22));
  191. P(C, D, E, A, B, R(23));
  192. P(B, C, D, E, A, R(24));
  193. P(A, B, C, D, E, R(25));
  194. P(E, A, B, C, D, R(26));
  195. P(D, E, A, B, C, R(27));
  196. P(C, D, E, A, B, R(28));
  197. P(B, C, D, E, A, R(29));
  198. P(A, B, C, D, E, R(30));
  199. P(E, A, B, C, D, R(31));
  200. P(D, E, A, B, C, R(32));
  201. P(C, D, E, A, B, R(33));
  202. P(B, C, D, E, A, R(34));
  203. P(A, B, C, D, E, R(35));
  204. P(E, A, B, C, D, R(36));
  205. P(D, E, A, B, C, R(37));
  206. P(C, D, E, A, B, R(38));
  207. P(B, C, D, E, A, R(39));
  208.  
  209. #undef K
  210. #undef F
  211.  
  212. #define F(x,y,z) ((x & y) | (z & (x | y)))
  213. #define K 0x8F1BBCDC
  214.  
  215. P(A, B, C, D, E, R(40));
  216. P(E, A, B, C, D, R(41));
  217. P(D, E, A, B, C, R(42));
  218. P(C, D, E, A, B, R(43));
  219. P(B, C, D, E, A, R(44));
  220. P(A, B, C, D, E, R(45));
  221. P(E, A, B, C, D, R(46));
  222. P(D, E, A, B, C, R(47));
  223. P(C, D, E, A, B, R(48));
  224. P(B, C, D, E, A, R(49));
  225. P(A, B, C, D, E, R(50));
  226. P(E, A, B, C, D, R(51));
  227. P(D, E, A, B, C, R(52));
  228. P(C, D, E, A, B, R(53));
  229. P(B, C, D, E, A, R(54));
  230. P(A, B, C, D, E, R(55));
  231. P(E, A, B, C, D, R(56));
  232. P(D, E, A, B, C, R(57));
  233. P(C, D, E, A, B, R(58));
  234. P(B, C, D, E, A, R(59));
  235.  
  236. #undef K
  237. #undef F
  238.  
  239. #define F(x,y,z) (x ^ y ^ z)
  240. #define K 0xCA62C1D6
  241.  
  242. P(A, B, C, D, E, R(60));
  243. P(E, A, B, C, D, R(61));
  244. P(D, E, A, B, C, R(62));
  245. P(C, D, E, A, B, R(63));
  246. P(B, C, D, E, A, R(64));
  247. P(A, B, C, D, E, R(65));
  248. P(E, A, B, C, D, R(66));
  249. P(D, E, A, B, C, R(67));
  250. P(C, D, E, A, B, R(68));
  251. P(B, C, D, E, A, R(69));
  252. P(A, B, C, D, E, R(70));
  253. P(E, A, B, C, D, R(71));
  254. P(D, E, A, B, C, R(72));
  255. P(C, D, E, A, B, R(73));
  256. P(B, C, D, E, A, R(74));
  257. P(A, B, C, D, E, R(75));
  258. P(E, A, B, C, D, R(76));
  259. P(D, E, A, B, C, R(77));
  260. P(C, D, E, A, B, R(78));
  261. P(B, C, D, E, A, R(79));
  262.  
  263. #undef K
  264. #undef F
  265.  
  266. A += INIT_SHA1_A;
  267. B += INIT_SHA1_B;
  268. C += INIT_SHA1_C;
  269. D += INIT_SHA1_D;
  270. E += INIT_SHA1_E;
  271.  
  272. state[0] = A;
  273. state[1] = B;
  274. state[2] = C;
  275. state[3] = D;
  276. state[4] = E;
  277.  
  278. // process buf (2nd part of stream)
  279. GET_WORD_32_BE(W[ 0], buf, 0);
  280. GET_WORD_32_BE(W[ 1], buf, 4);
  281. GET_WORD_32_BE(W[ 2], buf, 8);
  282. GET_WORD_32_BE(W[ 3], buf, 12);
  283. GET_WORD_32_BE(W[ 4], buf, 16);
  284. GET_WORD_32_BE(W[ 5], buf, 20);
  285. GET_WORD_32_BE(W[ 6], buf, 24);
  286. GET_WORD_32_BE(W[ 7], buf, 28);
  287. GET_WORD_32_BE(W[ 8], buf, 32);
  288. GET_WORD_32_BE(W[ 9], buf, 36);
  289. GET_WORD_32_BE(W[10], buf, 40);
  290. GET_WORD_32_BE(W[11], buf, 44);
  291. GET_WORD_32_BE(W[12], buf, 48);
  292. GET_WORD_32_BE(W[13], buf, 52);
  293. GET_WORD_32_BE(W[14], buf, 56);
  294. GET_WORD_32_BE(W[15], buf, 60);
  295.  
  296. #define F(x,y,z) (z ^ (x & (y ^ z)))
  297. #define K 0x5A827999
  298.  
  299. P(A, B, C, D, E, W[0] );
  300. P(E, A, B, C, D, W[1] );
  301. P(D, E, A, B, C, W[2] );
  302. P(C, D, E, A, B, W[3] );
  303. P(B, C, D, E, A, W[4] );
  304. P(A, B, C, D, E, W[5] );
  305. P(E, A, B, C, D, W[6] );
  306. P(D, E, A, B, C, W[7] );
  307. P(C, D, E, A, B, W[8] );
  308. P(B, C, D, E, A, W[9] );
  309. P(A, B, C, D, E, W[10]);
  310. P(E, A, B, C, D, W[11]);
  311. P(D, E, A, B, C, W[12]);
  312. P(C, D, E, A, B, W[13]);
  313. P(B, C, D, E, A, W[14]);
  314. P(A, B, C, D, E, W[15]);
  315. P(E, A, B, C, D, R(16));
  316. P(D, E, A, B, C, R(17));
  317. P(C, D, E, A, B, R(18));
  318. P(B, C, D, E, A, R(19));
  319.  
  320. #undef K
  321. #undef F
  322.  
  323. #define F(x,y,z) (x ^ y ^ z)
  324. #define K 0x6ED9EBA1
  325.  
  326. P(A, B, C, D, E, R(20));
  327. P(E, A, B, C, D, R(21));
  328. P(D, E, A, B, C, R(22));
  329. P(C, D, E, A, B, R(23));
  330. P(B, C, D, E, A, R(24));
  331. P(A, B, C, D, E, R(25));
  332. P(E, A, B, C, D, R(26));
  333. P(D, E, A, B, C, R(27));
  334. P(C, D, E, A, B, R(28));
  335. P(B, C, D, E, A, R(29));
  336. P(A, B, C, D, E, R(30));
  337. P(E, A, B, C, D, R(31));
  338. P(D, E, A, B, C, R(32));
  339. P(C, D, E, A, B, R(33));
  340. P(B, C, D, E, A, R(34));
  341. P(A, B, C, D, E, R(35));
  342. P(E, A, B, C, D, R(36));
  343. P(D, E, A, B, C, R(37));
  344. P(C, D, E, A, B, R(38));
  345. P(B, C, D, E, A, R(39));
  346.  
  347. #undef K
  348. #undef F
  349.  
  350. #define F(x,y,z) ((x & y) | (z & (x | y)))
  351. #define K 0x8F1BBCDC
  352.  
  353. P(A, B, C, D, E, R(40));
  354. P(E, A, B, C, D, R(41));
  355. P(D, E, A, B, C, R(42));
  356. P(C, D, E, A, B, R(43));
  357. P(B, C, D, E, A, R(44));
  358. P(A, B, C, D, E, R(45));
  359. P(E, A, B, C, D, R(46));
  360. P(D, E, A, B, C, R(47));
  361. P(C, D, E, A, B, R(48));
  362. P(B, C, D, E, A, R(49));
  363. P(A, B, C, D, E, R(50));
  364. P(E, A, B, C, D, R(51));
  365. P(D, E, A, B, C, R(52));
  366. P(C, D, E, A, B, R(53));
  367. P(B, C, D, E, A, R(54));
  368. P(A, B, C, D, E, R(55));
  369. P(E, A, B, C, D, R(56));
  370. P(D, E, A, B, C, R(57));
  371. P(C, D, E, A, B, R(58));
  372. P(B, C, D, E, A, R(59));
  373.  
  374. #undef K
  375. #undef F
  376.  
  377. #define F(x,y,z) (x ^ y ^ z)
  378. #define K 0xCA62C1D6
  379.  
  380. P(A, B, C, D, E, R(60));
  381. P(E, A, B, C, D, R(61));
  382. P(D, E, A, B, C, R(62));
  383. P(C, D, E, A, B, R(63));
  384. P(B, C, D, E, A, R(64));
  385. P(A, B, C, D, E, R(65));
  386. P(E, A, B, C, D, R(66));
  387. P(D, E, A, B, C, R(67));
  388. P(C, D, E, A, B, R(68));
  389. P(B, C, D, E, A, R(69));
  390. P(A, B, C, D, E, R(70));
  391. P(E, A, B, C, D, R(71));
  392. P(D, E, A, B, C, R(72));
  393. P(C, D, E, A, B, R(73));
  394. P(B, C, D, E, A, R(74));
  395. P(A, B, C, D, E, R(75));
  396. P(E, A, B, C, D, R(76));
  397. P(D, E, A, B, C, R(77));
  398. P(C, D, E, A, B, R(78));
  399. P(B, C, D, E, A, R(79));
  400.  
  401. #undef K
  402. #undef F
  403.  
  404. A += state[0];
  405. B += state[1];
  406. C += state[2];
  407. D += state[3];
  408. E += state[4];
  409.  
  410. PUT_WORD_32_BE(A, buf, 0);
  411. PUT_WORD_32_BE(B, buf, 4);
  412. PUT_WORD_32_BE(C, buf, 8);
  413. PUT_WORD_32_BE(D, buf, 12);
  414. PUT_WORD_32_BE(E, buf, 16);
  415.  
  416. buf[20] = 0x80;
  417. PUT_WORD_32_BE(0x2A0, buf, 60);
  418.  
  419. // step 6: append the stream of data 'text' to the B byte sting resulting from step 2
  420. // first part of stream (64 bytes) is opad, second part of stream (64 bytes) is the H result from step 4
  421.  
  422. // step 7: apply H to the stream (opad & buf) generated in step 6 and output the result
  423. GET_WORD_32_BE(W[ 0], opad, 0);
  424. GET_WORD_32_BE(W[ 1], opad, 4);
  425. GET_WORD_32_BE(W[ 2], opad, 8);
  426. GET_WORD_32_BE(W[ 3], opad, 12);
  427. GET_WORD_32_BE(W[ 4], opad, 16);
  428. GET_WORD_32_BE(W[ 5], opad, 20);
  429. GET_WORD_32_BE(W[ 6], opad, 24);
  430. GET_WORD_32_BE(W[ 7], opad, 28);
  431. GET_WORD_32_BE(W[ 8], opad, 32);
  432. GET_WORD_32_BE(W[ 9], opad, 36);
  433. GET_WORD_32_BE(W[10], opad, 40);
  434. GET_WORD_32_BE(W[11], opad, 44);
  435. GET_WORD_32_BE(W[12], opad, 48);
  436. GET_WORD_32_BE(W[13], opad, 52);
  437. GET_WORD_32_BE(W[14], opad, 56);
  438. GET_WORD_32_BE(W[15], opad, 60);
  439.  
  440. A = INIT_SHA1_A;
  441. B = INIT_SHA1_B;
  442. C = INIT_SHA1_C;
  443. D = INIT_SHA1_D;
  444. E = INIT_SHA1_E;
  445.  
  446. #define F(x,y,z) (z ^ (x & (y ^ z)))
  447. #define K 0x5A827999
  448.  
  449. P(A, B, C, D, E, W[0] );
  450. P(E, A, B, C, D, W[1] );
  451. P(D, E, A, B, C, W[2] );
  452. P(C, D, E, A, B, W[3] );
  453. P(B, C, D, E, A, W[4] );
  454. P(A, B, C, D, E, W[5] );
  455. P(E, A, B, C, D, W[6] );
  456. P(D, E, A, B, C, W[7] );
  457. P(C, D, E, A, B, W[8] );
  458. P(B, C, D, E, A, W[9] );
  459. P(A, B, C, D, E, W[10]);
  460. P(E, A, B, C, D, W[11]);
  461. P(D, E, A, B, C, W[12]);
  462. P(C, D, E, A, B, W[13]);
  463. P(B, C, D, E, A, W[14]);
  464. P(A, B, C, D, E, W[15]);
  465. P(E, A, B, C, D, R(16));
  466. P(D, E, A, B, C, R(17));
  467. P(C, D, E, A, B, R(18));
  468. P(B, C, D, E, A, R(19));
  469.  
  470. #undef K
  471. #undef F
  472.  
  473. #define F(x,y,z) (x ^ y ^ z)
  474. #define K 0x6ED9EBA1
  475.  
  476. P(A, B, C, D, E, R(20));
  477. P(E, A, B, C, D, R(21));
  478. P(D, E, A, B, C, R(22));
  479. P(C, D, E, A, B, R(23));
  480. P(B, C, D, E, A, R(24));
  481. P(A, B, C, D, E, R(25));
  482. P(E, A, B, C, D, R(26));
  483. P(D, E, A, B, C, R(27));
  484. P(C, D, E, A, B, R(28));
  485. P(B, C, D, E, A, R(29));
  486. P(A, B, C, D, E, R(30));
  487. P(E, A, B, C, D, R(31));
  488. P(D, E, A, B, C, R(32));
  489. P(C, D, E, A, B, R(33));
  490. P(B, C, D, E, A, R(34));
  491. P(A, B, C, D, E, R(35));
  492. P(E, A, B, C, D, R(36));
  493. P(D, E, A, B, C, R(37));
  494. P(C, D, E, A, B, R(38));
  495. P(B, C, D, E, A, R(39));
  496.  
  497. #undef K
  498. #undef F
  499.  
  500. #define F(x,y,z) ((x & y) | (z & (x | y)))
  501. #define K 0x8F1BBCDC
  502.  
  503. P(A, B, C, D, E, R(40));
  504. P(E, A, B, C, D, R(41));
  505. P(D, E, A, B, C, R(42));
  506. P(C, D, E, A, B, R(43));
  507. P(B, C, D, E, A, R(44));
  508. P(A, B, C, D, E, R(45));
  509. P(E, A, B, C, D, R(46));
  510. P(D, E, A, B, C, R(47));
  511. P(C, D, E, A, B, R(48));
  512. P(B, C, D, E, A, R(49));
  513. P(A, B, C, D, E, R(50));
  514. P(E, A, B, C, D, R(51));
  515. P(D, E, A, B, C, R(52));
  516. P(C, D, E, A, B, R(53));
  517. P(B, C, D, E, A, R(54));
  518. P(A, B, C, D, E, R(55));
  519. P(E, A, B, C, D, R(56));
  520. P(D, E, A, B, C, R(57));
  521. P(C, D, E, A, B, R(58));
  522. P(B, C, D, E, A, R(59));
  523.  
  524. #undef K
  525. #undef F
  526.  
  527. #define F(x,y,z) (x ^ y ^ z)
  528. #define K 0xCA62C1D6
  529.  
  530. P(A, B, C, D, E, R(60));
  531. P(E, A, B, C, D, R(61));
  532. P(D, E, A, B, C, R(62));
  533. P(C, D, E, A, B, R(63));
  534. P(B, C, D, E, A, R(64));
  535. P(A, B, C, D, E, R(65));
  536. P(E, A, B, C, D, R(66));
  537. P(D, E, A, B, C, R(67));
  538. P(C, D, E, A, B, R(68));
  539. P(B, C, D, E, A, R(69));
  540. P(A, B, C, D, E, R(70));
  541. P(E, A, B, C, D, R(71));
  542. P(D, E, A, B, C, R(72));
  543. P(C, D, E, A, B, R(73));
  544. P(B, C, D, E, A, R(74));
  545. P(A, B, C, D, E, R(75));
  546. P(E, A, B, C, D, R(76));
  547. P(D, E, A, B, C, R(77));
  548. P(C, D, E, A, B, R(78));
  549. P(B, C, D, E, A, R(79));
  550.  
  551. #undef K
  552. #undef F
  553.  
  554. A += INIT_SHA1_A;
  555. B += INIT_SHA1_B;
  556. C += INIT_SHA1_C;
  557. D += INIT_SHA1_D;
  558. E += INIT_SHA1_E;
  559.  
  560. // store state for 2nd part
  561. state[0] = A;
  562. state[1] = B;
  563. state[2] = C;
  564. state[3] = D;
  565. state[4] = E;
  566.  
  567. GET_WORD_32_BE(W[ 0], buf, 0);
  568. GET_WORD_32_BE(W[ 1], buf, 4);
  569. GET_WORD_32_BE(W[ 2], buf, 8);
  570. GET_WORD_32_BE(W[ 3], buf, 12);
  571. GET_WORD_32_BE(W[ 4], buf, 16);
  572. GET_WORD_32_BE(W[ 5], buf, 20);
  573. GET_WORD_32_BE(W[ 6], buf, 24);
  574. GET_WORD_32_BE(W[ 7], buf, 28);
  575. GET_WORD_32_BE(W[ 8], buf, 32);
  576. GET_WORD_32_BE(W[ 9], buf, 36);
  577. GET_WORD_32_BE(W[10], buf, 40);
  578. GET_WORD_32_BE(W[11], buf, 44);
  579. GET_WORD_32_BE(W[12], buf, 48);
  580. GET_WORD_32_BE(W[13], buf, 52);
  581. GET_WORD_32_BE(W[14], buf, 56);
  582. GET_WORD_32_BE(W[15], buf, 60);
  583.  
  584. #define F(x,y,z) (z ^ (x & (y ^ z)))
  585. #define K 0x5A827999
  586.  
  587. P(A, B, C, D, E, W[0] );
  588. P(E, A, B, C, D, W[1] );
  589. P(D, E, A, B, C, W[2] );
  590. P(C, D, E, A, B, W[3] );
  591. P(B, C, D, E, A, W[4] );
  592. P(A, B, C, D, E, W[5] );
  593. P(E, A, B, C, D, W[6] );
  594. P(D, E, A, B, C, W[7] );
  595. P(C, D, E, A, B, W[8] );
  596. P(B, C, D, E, A, W[9] );
  597. P(A, B, C, D, E, W[10]);
  598. P(E, A, B, C, D, W[11]);
  599. P(D, E, A, B, C, W[12]);
  600. P(C, D, E, A, B, W[13]);
  601. P(B, C, D, E, A, W[14]);
  602. P(A, B, C, D, E, W[15]);
  603. P(E, A, B, C, D, R(16));
  604. P(D, E, A, B, C, R(17));
  605. P(C, D, E, A, B, R(18));
  606. P(B, C, D, E, A, R(19));
  607.  
  608. #undef K
  609. #undef F
  610.  
  611. #define F(x,y,z) (x ^ y ^ z)
  612. #define K 0x6ED9EBA1
  613.  
  614. P(A, B, C, D, E, R(20));
  615. P(E, A, B, C, D, R(21));
  616. P(D, E, A, B, C, R(22));
  617. P(C, D, E, A, B, R(23));
  618. P(B, C, D, E, A, R(24));
  619. P(A, B, C, D, E, R(25));
  620. P(E, A, B, C, D, R(26));
  621. P(D, E, A, B, C, R(27));
  622. P(C, D, E, A, B, R(28));
  623. P(B, C, D, E, A, R(29));
  624. P(A, B, C, D, E, R(30));
  625. P(E, A, B, C, D, R(31));
  626. P(D, E, A, B, C, R(32));
  627. P(C, D, E, A, B, R(33));
  628. P(B, C, D, E, A, R(34));
  629. P(A, B, C, D, E, R(35));
  630. P(E, A, B, C, D, R(36));
  631. P(D, E, A, B, C, R(37));
  632. P(C, D, E, A, B, R(38));
  633. P(B, C, D, E, A, R(39));
  634.  
  635. #undef K
  636. #undef F
  637.  
  638. #define F(x,y,z) ((x & y) | (z & (x | y)))
  639. #define K 0x8F1BBCDC
  640.  
  641. P(A, B, C, D, E, R(40));
  642. P(E, A, B, C, D, R(41));
  643. P(D, E, A, B, C, R(42));
  644. P(C, D, E, A, B, R(43));
  645. P(B, C, D, E, A, R(44));
  646. P(A, B, C, D, E, R(45));
  647. P(E, A, B, C, D, R(46));
  648. P(D, E, A, B, C, R(47));
  649. P(C, D, E, A, B, R(48));
  650. P(B, C, D, E, A, R(49));
  651. P(A, B, C, D, E, R(50));
  652. P(E, A, B, C, D, R(51));
  653. P(D, E, A, B, C, R(52));
  654. P(C, D, E, A, B, R(53));
  655. P(B, C, D, E, A, R(54));
  656. P(A, B, C, D, E, R(55));
  657. P(E, A, B, C, D, R(56));
  658. P(D, E, A, B, C, R(57));
  659. P(C, D, E, A, B, R(58));
  660. P(B, C, D, E, A, R(59));
  661.  
  662. #undef K
  663. #undef F
  664.  
  665. #define F(x,y,z) (x ^ y ^ z)
  666. #define K 0xCA62C1D6
  667.  
  668. P(A, B, C, D, E, R(60));
  669. P(E, A, B, C, D, R(61));
  670. P(D, E, A, B, C, R(62));
  671. P(C, D, E, A, B, R(63));
  672. P(B, C, D, E, A, R(64));
  673. P(A, B, C, D, E, R(65));
  674. P(E, A, B, C, D, R(66));
  675. P(D, E, A, B, C, R(67));
  676. P(C, D, E, A, B, R(68));
  677. P(B, C, D, E, A, R(69));
  678. P(A, B, C, D, E, R(70));
  679. P(E, A, B, C, D, R(71));
  680. P(D, E, A, B, C, R(72));
  681. P(C, D, E, A, B, R(73));
  682. P(B, C, D, E, A, R(74));
  683. P(A, B, C, D, E, R(75));
  684. P(E, A, B, C, D, R(76));
  685. P(D, E, A, B, C, R(77));
  686. P(C, D, E, A, B, R(78));
  687. P(B, C, D, E, A, R(79));
  688.  
  689. #undef K
  690. #undef F
  691.  
  692. A += state[0];
  693. B += state[1];
  694. C += state[2];
  695. D += state[3];
  696. E += state[4];
  697.  
  698. PUT_WORD_32_BE(A, output, 0);
  699. PUT_WORD_32_BE(B, output, 4);
  700. PUT_WORD_32_BE(C, output, 8);
  701. PUT_WORD_32_BE(D, output, 12);
  702. PUT_WORD_32_BE(E, output, 16);
  703. }
  704.  
  705.  
  706. /* PBKDF2
  707.  * stripped-down implementation
  708.  * based on the source code written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL Project 1999
  709.  */
  710. static void PBKDF2_DCC2(const unsigned char *pass, const unsigned char *salt, int saltlen, unsigned char *out)
  711. {
  712. unsigned char temp[SHA1_DIGEST_LENGTH];
  713. unsigned char buf[48];
  714. unsigned int i;
  715.  
  716. memset(buf, 0, 48);
  717. memcpy(buf, salt, saltlen);
  718. buf[saltlen + 3] = 0x01;
  719.  
  720. hmac_sha1(pass, 16, buf, saltlen + 4, temp);
  721.  
  722. memcpy(out, temp, 16);
  723.  
  724. for (i = 1; i < ITERATIONS; i++)
  725. {
  726. hmac_sha1(pass, 16, temp, SHA1_DIGEST_LENGTH, temp);
  727.  
  728. out[ 0] ^= temp[0];
  729. out[ 1] ^= temp[1];
  730. out[ 2] ^= temp[2];
  731. out[ 3] ^= temp[3];
  732. out[ 4] ^= temp[4];
  733. out[ 5] ^= temp[5];
  734. out[ 6] ^= temp[6];
  735. out[ 7] ^= temp[7];
  736. out[ 8] ^= temp[8];
  737. out[ 9] ^= temp[9];
  738. out[10] ^= temp[10];
  739. out[11] ^= temp[11];
  740. out[12] ^= temp[12];
  741. out[13] ^= temp[13];
  742. out[14] ^= temp[14];
  743. out[15] ^= temp[15];
  744. // out[16] ^= temp[16]; - was a bug?
  745. }
  746. }
  747.  
  748.  
  749.  
  750. // MD4 compression function
  751. void md4_crypt(unsigned int *buffer, unsigned int *hash)
  752. {
  753. unsigned int a;
  754. unsigned int b;
  755. unsigned int c;
  756. unsigned int d;
  757.  
  758. // round 1
  759. a = 0xFFFFFFFF + buffer[0]; a = (a << 3 ) | (a >> 29);
  760. d = INIT_MD4_D + (INIT_MD4_C ^ (a & 0x77777777)) + buffer[1]; d = (d << 7 ) | (d >> 25);
  761. c = INIT_MD4_C + (INIT_MD4_B ^ (d & (a ^ INIT_MD4_B))) + buffer[2]; c = (c << 11) | (c >> 21);
  762. b = INIT_MD4_B + (a ^ (c & (d ^ a))) + buffer[3]; b = (b << 19) | (b >> 13);
  763.  
  764. a += (d ^ (b & (c ^ d))) + buffer[4]; a = (a << 3 ) | (a >> 29);
  765. d += (c ^ (a & (b ^ c))) + buffer[5]; d = (d << 7 ) | (d >> 25);
  766. c += (b ^ (d & (a ^ b))) + buffer[6]; c = (c << 11) | (c >> 21);
  767. b += (a ^ (c & (d ^ a))) + buffer[7]; b = (b << 19) | (b >> 13);
  768.  
  769. a += (d ^ (b & (c ^ d))) + buffer[8] ; a = (a << 3 ) | (a >> 29);
  770. d += (c ^ (a & (b ^ c))) + buffer[9] ; d = (d << 7 ) | (d >> 25);
  771. c += (b ^ (d & (a ^ b))) + buffer[10]; c = (c << 11) | (c >> 21);
  772. b += (a ^ (c & (d ^ a))) + buffer[11]; b = (b << 19) | (b >> 13);
  773.  
  774. a += (d ^ (b & (c ^ d))) + buffer[12]; a = (a << 3 ) | (a >> 29);
  775. d += (c ^ (a & (b ^ c))) + buffer[13]; d = (d << 7 ) | (d >> 25);
  776. c += (b ^ (d & (a ^ b))) + buffer[14]; c = (c << 11) | (c >> 21);
  777. b += (a ^ (c & (d ^ a))) + buffer[15]; b = (b << 19) | (b >> 13);
  778.  
  779. // round 2
  780. a += ((b & (c | d)) | (c & d)) + buffer[0] + SQRT_2; a = (a<<3 ) | (a>>29);
  781. d += ((a & (b | c)) | (b & c)) + buffer[4] + SQRT_2; d = (d<<5 ) | (d>>27);
  782. c += ((d & (a | b)) | (a & b)) + buffer[8] + SQRT_2; c = (c<<9 ) | (c>>23);
  783. b += ((c & (d | a)) | (d & a)) + buffer[12] + SQRT_2; b = (b<<13) | (b>>19);
  784.  
  785. a += ((b & (c | d)) | (c & d)) + buffer[1] + SQRT_2; a = (a<<3 ) | (a>>29);
  786. d += ((a & (b | c)) | (b & c)) + buffer[5] + SQRT_2; d = (d<<5 ) | (d>>27);
  787. c += ((d & (a | b)) | (a & b)) + buffer[9] + SQRT_2; c = (c<<9 ) | (c>>23);
  788. b += ((c & (d | a)) | (d & a)) + buffer[13] + SQRT_2; b = (b<<13) | (b>>19);
  789.  
  790. a += ((b & (c | d)) | (c & d)) + buffer[2] + SQRT_2; a = (a<<3 ) | (a>>29);
  791. d += ((a & (b | c)) | (b & c)) + buffer[6] + SQRT_2; d = (d<<5 ) | (d>>27);
  792. c += ((d & (a | b)) | (a & b)) + buffer[10] + SQRT_2; c = (c<<9 ) | (c>>23);
  793. b += ((c & (d | a)) | (d & a)) + buffer[14] + SQRT_2; b = (b<<13) | (b>>19);
  794.  
  795. a += ((b & (c | d)) | (c & d)) + buffer[3] + SQRT_2; a = (a<<3 ) | (a>>29);
  796. d += ((a & (b | c)) | (b & c)) + buffer[7] + SQRT_2; d = (d<<5 ) | (d>>27);
  797. c += ((d & (a | b)) | (a & b)) + buffer[11] + SQRT_2; c = (c<<9 ) | (c>>23);
  798. b += ((c & (d | a)) | (d & a)) + buffer[15] + SQRT_2; b = (b<<13) | (b>>19);
  799.  
  800. // round 3
  801. a += (d ^ c ^ b) + buffer[0] + SQRT_3; a = (a << 3 ) | (a >> 29);
  802. d += (c ^ b ^ a) + buffer[8] + SQRT_3; d = (d << 9 ) | (d >> 23);
  803. c += (b ^ a ^ d) + buffer[4] + SQRT_3; c = (c << 11) | (c >> 21);
  804. b += (a ^ d ^ c) + buffer[12] + SQRT_3; b = (b << 15) | (b >> 17);
  805.  
  806. a += (d ^ c ^ b) + buffer[2] + SQRT_3; a = (a << 3 ) | (a >> 29);
  807. d += (c ^ b ^ a) + buffer[10] + SQRT_3; d = (d << 9 ) | (d >> 23);
  808. c += (b ^ a ^ d) + buffer[6] + SQRT_3; c = (c << 11) | (c >> 21);
  809. b += (a ^ d ^ c) + buffer[14] + SQRT_3; b = (b << 15) | (b >> 17);
  810.  
  811. a += (d ^ c ^ b) + buffer[1] + SQRT_3; a = (a << 3 ) | (a >> 29);
  812. d += (c ^ b ^ a) + buffer[9] + SQRT_3; d = (d << 9 ) | (d >> 23);
  813. c += (b ^ a ^ d) + buffer[5] + SQRT_3; c = (c << 11) | (c >> 21);
  814. b += (a ^ d ^ c) + buffer[13] + SQRT_3; b = (b << 15) | (b >> 17);
  815.  
  816. a += (d ^ c ^ b) + buffer[3] + SQRT_3; a = (a << 3 ) | (a >> 29);
  817.  
  818. d += (c ^ b ^ a) + buffer[11] + SQRT_3; d = (d << 9 ) | (d >> 23);
  819. c += (b ^ a ^ d) + buffer[7] + SQRT_3; c = (c << 11) | (c >> 21);
  820. b += (a ^ d ^ c) + buffer[15] + SQRT_3; b = (b << 15) | (b >> 17);
  821.  
  822. hash[0] = a + INIT_MD4_A;
  823. hash[1] = b + INIT_MD4_B;
  824. hash[2] = c + INIT_MD4_C;
  825. hash[3] = d + INIT_MD4_D;
  826. }
  827.  
  828. // main
  829. int main(int argc, char *argv[])
  830. {
  831. unsigned int i;
  832. unsigned int buffer[16];
  833. unsigned int nt_hash[16];
  834. unsigned int dcc_hash[16];
  835. unsigned int dcc2_hash[16];
  836. unsigned char salt[44];
  837. unsigned char username[] = "test";
  838. unsigned char password[] = "password";
  839. unsigned int username_len = strlen(username);
  840. unsigned int password_len = strlen(password);
  841.  
  842. memset(nt_hash, 0, 64);
  843. memset(buffer, 0, 64);
  844. memset(salt, 0, 44);
  845.  
  846. // convert ASCII username to Unicode (WideChar)
  847. for(i = 0; i < (username_len >> 1) + 1; i++)
  848. ((unsigned int *)salt)[i] = username[2 * i] | (username[2 * i + 1] << 16);
  849.  
  850. // convert ASCII password to Unicode
  851.  
  852. for(i = 0; i < password_len >> 1; i++)
  853. buffer[i] = password[2 * i] | (password[2 * i + 1] << 16);
  854.  
  855. // MD4 padding
  856. if(password_len % 2 == 1)
  857. buffer[i] = password[password_len - 1] | 0x800000;
  858. else
  859. buffer[i]=0x80;
  860.  
  861. // put password length at end of buffer
  862. buffer[14] = password_len << 4;
  863.  
  864. // generate MD4 hash of the password (NT hash)
  865. md4_crypt(buffer, nt_hash);
  866.  
  867. // concatenate NT hash and the username (salt)
  868. memcpy((unsigned char *)nt_hash + 16, salt, username_len << 1);
  869.  
  870. i = username_len + 8;
  871.  
  872. // MD4 padding
  873. if(username_len % 2 == 1)
  874. nt_hash[i >> 1] = username[username_len - 1] | 0x800000;
  875. else
  876. nt_hash[i >> 1] = 0x80;
  877.  
  878. // put length at end of buffer
  879. nt_hash[14] = i << 4;
  880.  
  881. md4_crypt(nt_hash, dcc_hash);
  882.  
  883.  
  884. // stripped-down PBKDF2 for DCC2
  885. PBKDF2_DCC2((unsigned char*)dcc_hash, salt, username_len << 1, (unsigned char*)dcc2_hash);
  886.  
  887. // the even slower OpenSSL PBKDF2 implementation (compile with -lssl)
  888. // PKCS5_PBKDF2_HMAC_SHA1((unsigned char*)dcc_hash, 16, salt, username_len << 1, ITERATIONS, 16, (unsigned char*)dcc2_hash);
  889.  
  890. // user credentials and DCC and DCC2 hash values
  891. printf("username : %s\n", username);
  892. printf("password : %s\n", password);
  893. printf("DCC aka M$ Cache : %s\n", byte2hexstring((unsigned char *)dcc_hash, 16));
  894. printf("DCC2 aka M$ Cache 2: %s\n", byte2hexstring((unsigned char *)dcc2_hash, 16));
  895.  
  896. return 0;
  897. }
Success #stdin #stdout 0.01s 5296KB
stdin
/*  Berechnung des Hamming-Abstandes zwischen zwei 128-Bit Werten in 	*/
/*	einer Textdatei. 													*/
/*  Die Werte müssen auf einer separaten Zeile gespeichert sein			*/
/* 																		*/
/*	Erstellt: 17.5.2010													*/
/*  Autor: Thomas Scheffler												*/

#include <stdio.h>
#include <stdlib.h>

#define ARRAY_SIZE 32

unsigned Hamdist(unsigned x, unsigned y)
{
  unsigned dist = 0, val = x ^ y;
 
  // Count the number of set bits
  while(val)
  {
    ++dist; 
    val &= val - 1;
  }
 
  return dist;
}



int main (void)
{
	char hex;
	int i;
	int a[ARRAY_SIZE];
	int b[ARRAY_SIZE];
	int hamDist = 0;
	FILE* fp;
	
	//Arrays mit 0 initialisieren
	for (i = 0; i < ARRAY_SIZE; ++i)
	{
  		a[i] = 0;
  		b[i] = 0;
	}

	
	fp = fopen("hex.txt","r");
	if (fp == NULL) 
	{
		printf("Die Datei hex.txt wurde nicht gefunden!");
		exit(EXIT_FAILURE);
	}

	i=0;
	printf("1.Zeile einlesen.\n");

 	while((hex=fgetc(fp))!='\n' && hex != EOF)
    {
        a[i]=strtol(&hex,0,16);
		i++;
    }
	i=0;
	printf("2.Zeile einlesen.\n");

 	while((hex=fgetc(fp))!='\n' && hex != EOF)
    {
    	b[i]=strtol(&hex,0,16);
        i++;
    }
	fclose(fp);

	printf("Hamming-Abweichung pro Nibble:\n");
	for (i = 0; i < ARRAY_SIZE; ++i)
	{
		printf ("%i\t%i\t%i\n",a[i],b[i],Hamdist(a[i],b[i]));
		hamDist += Hamdist(a[i],b[i]);
	}
	printf ("\nHamming-Abweichung der Hash-Werte:%d\n",hamDist);
}

stdout
username           : test
password           : password
DCC  aka M$ Cache  : 1f34a7dfade9ddaa26746086ecbe77bf
DCC2 aka M$ Cache 2: a86012faf7d88d1fc037a69764a92cac