Openwall GNU/*/Linux - a small security-enhanced Linux distro for servers
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Date: Sat, 21 Jul 2012 02:42:30 +0530
From: deepika <deepikadutta_19@...oo.com>
To: john-dev@...ts.openwall.com
Subject: mschapv2-bitsliced conversion- openmp support

Hi solar & magnum,
I have added support for openmp in mschapv2. The patch is attached. 
Following benchmark results were achieved on my Intel core 2 duo laptop.


**Without OpenMP
--With BS
Benchmarking: MSCHAPv2 C/R MD4 DES [DES_BS_MSCHAPv2]... DONE
Many salts:     7344K c/s real, 7344K c/s virtual
Only one salt:  2320K c/s real, 2320K c/s virtual

--Without BS
Benchmarking: MSCHAPv2 C/R MD4 DES [mschapv2]... DONE
Many salts:     1492K c/s real, 1492K c/s virtual
Only one salt:  1051K c/s real, 1040K c/s virtual

**With OpenMP
--With BS
Benchmarking: MSCHAPv2 C/R MD4 DES [DES_BS_MSCHAPv2]... (2xOMP) DONE
Many salts:     12965K c/s real, 6482K c/s virtual
Only one salt:  4036K c/s real, 2018K c/s virtual

--Without BS
Benchmarking: MSCHAPv2 C/R MD4 DES [mschapv2]... (2xOMP) DONE
Many salts:     2844K c/s real, 1422K c/s virtual
Only one salt:  1937K c/s real, 963884 c/s virtual

-Deepika

diff -urpN john-1.7.9-jumbo-5.orig/src/DES_bs_b.c john-1.7.9-jumbo-5.final/src/DES_bs_b.c
--- john-1.7.9-jumbo-5.orig/src/DES_bs_b.c	2011-11-19 02:57:47.000000000 +0000
+++ john-1.7.9-jumbo-5.final/src/DES_bs_b.c	2012-07-19 15:40:01.961272000 +0000
@@ -1,6 +1,10 @@
 /*
  * This file is part of John the Ripper password cracker,
  * Copyright (c) 1996-2001,2003,2010,2011 by Solar Designer
+ *
+ * Addition of single DES encryption with no salt by
+ * Deepika Dutta Mishra <dipikadutta at gmail.com> in 2012, no
+ * rights reserved. 
  */
 
 #include "arch.h"
@@ -1438,13 +1442,6 @@ static MAYBE_INLINE void DES_bs_finalize
 	}
 }
 
-#undef v1
-#undef v2
-#undef v3
-#undef v5
-#undef v6
-#undef v7
-
 #undef kd
 #if DES_BS_VECTOR_LOOPS
 #define kd				[depth]
@@ -1561,3 +1558,150 @@ void DES_bs_crypt_LM(int keys_count)
 	}
 }
 #endif
+
+#if DES_bs_mt
+static MAYBE_INLINE void DES_bs_finalize_keys_plain(int t)
+#else
+static MAYBE_INLINE void DES_bs_finalize_keys_plain(void)
+#endif
+{
+#if DES_BS_VECTOR_LOOPS_K
+	int depth;
+#endif
+
+	for_each_depth_k() {
+      DES_bs_vector *kp = (DES_bs_vector *)&DES_bs_all.K[0] DEPTH_K;
+      int ic;
+      for (ic = 0; ic < 8; ic++) {
+               DES_bs_vector *vp =
+               (DES_bs_vector *)&DES_bs_all.xkeys.v[ic][0] DEPTH_K;
+               LOAD_V
+               FINALIZE_NEXT_KEY_BIT_0
+               FINALIZE_NEXT_KEY_BIT_1
+               FINALIZE_NEXT_KEY_BIT_2
+               FINALIZE_NEXT_KEY_BIT_3
+               FINALIZE_NEXT_KEY_BIT_4
+               FINALIZE_NEXT_KEY_BIT_5
+               FINALIZE_NEXT_KEY_BIT_6
+              }
+	}
+}
+
+#undef v1
+#undef v2
+#undef v3
+#undef v5
+#undef v6
+#undef v7
+
+
+/* Single Des Encryption with no salt */
+#undef kd
+#if DES_BS_VECTOR_LOOPS
+#define kd				[depth]
+#else
+#define kd				[0]
+#endif
+
+void DES_bs_crypt_plain(int keys_count)
+{
+#if DES_bs_mt
+	int t, n = (keys_count + (DES_BS_DEPTH - 1)) / DES_BS_DEPTH;
+#endif
+
+
+#ifdef _OPENMP
+#pragma omp parallel for default(none) private(t) shared(n, DES_bs_all_p, keys_count,Plaintext)
+#endif
+	for_each_t(n) {
+			ARCH_WORD **k;
+			int rounds;
+	#if DES_BS_VECTOR_LOOPS
+			int depth;
+	#endif
+			int i;
+			for(i=0; i<64; i++)
+					DES_bs_all.B[i] = Plaintext[i];
+
+#if DES_bs_mt
+		DES_bs_finalize_keys_plain(t);
+#else
+		DES_bs_finalize_keys_plain();
+#endif
+
+	k = DES_bs_all.KS.p;
+	rounds = 8;
+
+	do {
+		for_each_depth()
+		s1(y(31, 0), y(0, 1), y(1, 2),
+			y(2, 3), y(3, 4), y(4, 5),
+			z(40), z(48), z(54), z(62));
+		for_each_depth()
+		s2(y(3, 6), y(4, 7), y(5, 8),
+			y(6, 9), y(7, 10), y(8, 11),
+			z(44), z(59), z(33), z(49));
+		for_each_depth()
+		s3(y(7, 12), y(8, 13), y(9, 14),
+			y(10, 15), y(11, 16), y(12, 17),
+			z(55), z(47), z(61), z(37));
+		for_each_depth()
+		s4(y(11, 18), y(12, 19), y(13, 20),
+			y(14, 21), y(15, 22), y(16, 23),
+			z(57), z(51), z(41), z(32));
+		for_each_depth()
+		s5(y(15, 24), y(16, 25), y(17, 26),
+			y(18, 27), y(19, 28), y(20, 29),
+			z(39), z(45), z(56), z(34));
+		for_each_depth()
+		s6(y(19, 30), y(20, 31), y(21, 32),
+			y(22, 33), y(23, 34), y(24, 35),
+			z(35), z(60), z(42), z(50));
+		for_each_depth()
+		s7(y(23, 36), y(24, 37), y(25, 38),
+			y(26, 39), y(27, 40), y(28, 41),
+			z(63), z(43), z(53), z(38));
+		for_each_depth()
+		s8(y(27, 42), y(28, 43), y(29, 44),
+			y(30, 45), y(31, 46), y(0, 47),
+			z(36), z(58), z(46), z(52));
+
+		for_each_depth()
+		s1(y(63, 48), y(32, 49), y(33, 50),
+			y(34, 51), y(35, 52), y(36, 53),
+			z(8), z(16), z(22), z(30));
+		for_each_depth()
+		s2(y(35, 54), y(36, 55), y(37, 56),
+			y(38, 57), y(39, 58), y(40, 59),
+			z(12), z(27), z(1), z(17));
+		for_each_depth()
+		s3(y(39, 60), y(40, 61), y(41, 62),
+			y(42, 63), y(43, 64), y(44, 65),
+			z(23), z(15), z(29), z(5));
+		for_each_depth()
+		s4(y(43, 66), y(44, 67), y(45, 68),
+			y(46, 69), y(47, 70), y(48, 71),
+			z(25), z(19), z(9), z(0));
+		for_each_depth()
+		s5(y(47, 72), y(48, 73), y(49, 74),
+			y(50, 75), y(51, 76), y(52, 77),
+			z(7), z(13), z(24), z(2));
+		for_each_depth()
+		s6(y(51, 78), y(52, 79), y(53, 80),
+			y(54, 81), y(55, 82), y(56, 83),
+			z(3), z(28), z(10), z(18));
+		for_each_depth()
+		s7(y(55, 84), y(56, 85), y(57, 86),
+			y(58, 87), y(59, 88), y(60, 89),
+			z(31), z(11), z(21), z(6));
+		for_each_depth()
+		s8(y(59, 90), y(60, 91), y(61, 92),
+			y(62, 93), y(63, 94), y(32, 95),
+			z(4), z(26), z(14), z(20));
+
+		k += 96;
+	} while (--rounds);
+}}
+
+
+
diff -urpN john-1.7.9-jumbo-5.orig/src/DES_bs.c john-1.7.9-jumbo-5.final/src/DES_bs.c
--- john-1.7.9-jumbo-5.orig/src/DES_bs.c	2011-12-15 20:57:19.000000000 +0000
+++ john-1.7.9-jumbo-5.final/src/DES_bs.c	2012-07-11 19:12:11.471315000 +0000
@@ -1,6 +1,11 @@
 /*
  * This file is part of John the Ripper password cracker,
  * Copyright (c) 1996-2002,2005,2010,2011 by Solar Designer
+ *
+ * Modified for adding support for single DES encryption with 
+ * no salt by Deepika Dutta Mishra <dipikadutta at gmail.com> in 
+ * 2012, no rights reserved.
+ *
  */
 
 #include <string.h>
@@ -36,7 +41,7 @@ DES_bs_combined *DES_bs_all_p = NULL;
 #elif !DES_BS_ASM
 DES_bs_combined CC_CACHE_ALIGN DES_bs_all;
 #endif
-
+DES_bs_vector Plaintext[64];
 static unsigned char DES_LM_KP[56] = {
 	1, 2, 3, 4, 5, 6, 7,
 	10, 11, 12, 13, 14, 15, 0,
@@ -101,6 +106,7 @@ void DES_bs_init(int LM, int cpt)
 			k = DES_bs_all.KS.p;
 		else
 			k = DES_bs_all.KSp;
+
 #else
 		k = DES_bs_all.KS.p;
 #endif
@@ -117,7 +123,7 @@ void DES_bs_init(int LM, int cpt)
 				bit ^= 070;
 				bit -= bit >> 3;
 				bit = 55 - bit;
-				if (LM) bit = DES_LM_KP[bit];
+				if (LM == 1) bit = DES_LM_KP[bit];
 				*k++ = &DES_bs_all.K[bit] START;
 			}
 		}
@@ -130,7 +136,7 @@ void DES_bs_init(int LM, int cpt)
 			DES_bs_all.pxkeys[index] =
 			    &DES_bs_all.xkeys.c[0][index & 7][index >> 3];
 
-		if (LM) {
+		if (LM ==1) {
 			for (c = 0; c < 0x100; c++)
 #ifdef BENCH_BUILD
 			if (c >= 'a' && c <= 'z')
@@ -140,7 +146,7 @@ void DES_bs_init(int LM, int cpt)
 #else
 			DES_bs_all.E.u[c] = CP_up[c];
 #endif
-		} else {
+		} else if(LM==0) {
 			for (index = 0; index < 48; index++)
 				DES_bs_all.Ens[index] =
 				    &DES_bs_all.B[DES_E[index]];
diff -urpN john-1.7.9-jumbo-5.orig/src/DES_bs.h john-1.7.9-jumbo-5.final/src/DES_bs.h
--- john-1.7.9-jumbo-5.orig/src/DES_bs.h	2011-11-23 06:54:50.000000000 +0000
+++ john-1.7.9-jumbo-5.final/src/DES_bs.h	2012-07-11 19:03:11.463673000 +0000
@@ -71,6 +71,10 @@ typedef struct {
 	DES_bs_vector *Ens[48];	/* Pointers into B[] for non-salted E */
 } DES_bs_combined;
 
+//store plaintext//
+extern DES_bs_vector Plaintext[64];
+
+
 #if defined(_OPENMP) && !DES_BS_ASM
 #define DES_bs_mt			1
 #define DES_bs_cpt			32
@@ -180,4 +184,5 @@ extern int DES_bs_cmp_all(ARCH_WORD *bin
  */
 extern int DES_bs_cmp_one(ARCH_WORD *binary, int count, int index);
 
+extern void DES_bs_crypt_plain(int keys_count);
 #endif
diff -urpN john-1.7.9-jumbo-5.orig/src/DES_std.c john-1.7.9-jumbo-5.final/src/DES_std.c
--- john-1.7.9-jumbo-5.orig/src/DES_std.c	2011-12-15 20:57:19.000000000 +0000
+++ john-1.7.9-jumbo-5.final/src/DES_std.c	2012-07-11 19:03:11.463673000 +0000
@@ -269,7 +269,8 @@ unsigned char DES_E[48] = {
 	27, 28, 29, 30, 31, 0
 };
 
-static unsigned char DES_IP[64] = {
+
+unsigned char DES_IP[64] = {
 	57, 49, 41, 33, 25, 17, 9, 1,
 	59, 51, 43, 35, 27, 19, 11, 3,
 	61, 53, 45, 37, 29, 21, 13, 5,
diff -urpN john-1.7.9-jumbo-5.orig/src/DES_std.h john-1.7.9-jumbo-5.final/src/DES_std.h
--- john-1.7.9-jumbo-5.orig/src/DES_std.h	2011-12-15 20:57:19.000000000 +0000
+++ john-1.7.9-jumbo-5.final/src/DES_std.h	2012-07-11 19:03:11.465613000 +0000
@@ -25,7 +25,7 @@ extern unsigned char DES_E[48];
 extern unsigned char DES_PC1[56];
 extern unsigned char DES_ROT[16];
 extern unsigned char DES_PC2[48];
-
+extern unsigned char DES_IP[64];
 /*
  * Current key schedule.
  */
diff -urpN john-1.7.9-jumbo-5.orig/src/Makefile john-1.7.9-jumbo-5.final/src/Makefile
--- john-1.7.9-jumbo-5.orig/src/Makefile	2011-12-16 19:12:33.000000000 +0000
+++ john-1.7.9-jumbo-5.final/src/Makefile	2012-07-20 20:29:50.482071000 +0000
@@ -5,7 +5,7 @@
 # ...with changes in the jumbo patch, by various authors
 #
 
-CC = gcc
+CC = gcc 
 AS = $(CC)
 LD = $(CC)
 CPP = $(CC)
diff -urpN john-1.7.9-jumbo-5.orig/src/MSCHAPv2_fmt_plug.c john-1.7.9-jumbo-5.final/src/MSCHAPv2_fmt_plug.c
--- john-1.7.9-jumbo-5.orig/src/MSCHAPv2_fmt_plug.c	2011-12-15 22:24:44.000000000 +0000
+++ john-1.7.9-jumbo-5.final/src/MSCHAPv2_fmt_plug.c	2012-07-20 20:49:17.407002597 +0000
@@ -7,6 +7,10 @@
  * Modified for performance, OMP and utf-8 support
  * by magnum 2010-2011, no rights reserved
  *
+ * Modified for using Bitsliced DES by Deepika Dutta Mishra
+ * <dipikadutta at gmail.com> in 2012, no rights reserved. 
+ * Supports Openmp.
+ *
  * This algorithm is designed for performing brute-force cracking of the
  * MSCHAPv2 challenge/response sets exchanged during network-based
  * authentication attempts. The captured challenge/response set from these
@@ -23,7 +27,9 @@
  * http://freeradius.org/rfc/rfc2759.txt
  *
  */
-
+#include "arch.h"
+#include "DES_std.h"
+#include "DES_bs.h"
 #include <string.h>
 #ifdef _OPENMP
 #include <omp.h>
@@ -36,7 +42,6 @@
 #include "memory.h"
 
 #include "sha.h"
-#include <openssl/des.h>
 
 #ifndef uchar
 #define uchar unsigned char
@@ -44,7 +49,7 @@
 
 #define FORMAT_LABEL         "mschapv2"
 #define FORMAT_NAME          "MSCHAPv2 C/R MD4 DES"
-#define ALGORITHM_NAME       "mschapv2"
+#define ALGORITHM_NAME       "DES_BS_MSCHAPv2"
 #define BENCHMARK_COMMENT    ""
 #define BENCHMARK_LENGTH     0
 #define PLAINTEXT_LENGTH     125 /* lmcons.h - PWLEN (256) ? 127 ? */
@@ -58,13 +63,8 @@
 #define TOTAL_LENGTH         13 + USERNAME_LENGTH + CHALLENGE_LENGTH + CIPHERTEXT_LENGTH
 
 // these may be altered in init() if running OMP
-#define MIN_KEYS_PER_CRYPT	1
-#define THREAD_RATIO		256
-#ifdef _OPENMP
-#define MAX_KEYS_PER_CRYPT	0x10000
-#else
-#define MAX_KEYS_PER_CRYPT	THREAD_RATIO
-#endif
+#define MIN_KEYS_PER_CRYPT	DES_BS_DEPTH
+#define MAX_KEYS_PER_CRYPT      DES_BS_DEPTH
 
 static struct fmt_tests tests[] = {
   {"$MSCHAPv2$4c092fd3fd98236502e8591100046326$b912ce522524d33123a982cf330a57f8e953fa7974042b5d$6a4915d0ce61d42be533640a75391925$1111", "2222"},
@@ -86,30 +86,25 @@ static struct fmt_tests tests[] = {
 static uchar (*saved_plain)[PLAINTEXT_LENGTH + 1];
 static int (*saved_len);
 static uchar (*saved_key)[21];
-static uchar (*output)[PARTIAL_BINARY_SIZE];
 static uchar *challenge;
 static int keys_prepared;
+static void mschapv2_set_salt(void *salt);
 
 #include "unicode.h"
 
 static void init(struct fmt_main *pFmt)
 {
-#ifdef _OPENMP
-	int n = MIN_KEYS_PER_CRYPT * omp_get_max_threads();
-	if (n < MIN_KEYS_PER_CRYPT)
-		n = MIN_KEYS_PER_CRYPT;
-	if (n > MAX_KEYS_PER_CRYPT)
-		n = MAX_KEYS_PER_CRYPT;
-	pFmt->params.min_keys_per_crypt = n;
-	n = n * (n << 1) * THREAD_RATIO;
-	if (n > MAX_KEYS_PER_CRYPT)
-		n = MAX_KEYS_PER_CRYPT;
-	pFmt->params.max_keys_per_crypt = n;
-#endif
+
+	/* LM =2 for DES encryption with no salt and no iterations */
+	DES_bs_init(2, DES_bs_cpt);
+	#if DES_bs_mt
+		pFmt->params.min_keys_per_crypt = DES_bs_min_kpc;
+		pFmt->params.max_keys_per_crypt = DES_bs_max_kpc;
+	#endif
+
 	saved_plain = mem_calloc_tiny(sizeof(*saved_plain) * pFmt->params.max_keys_per_crypt, MEM_ALIGN_NONE);
 	saved_len = mem_calloc_tiny(sizeof(*saved_len) * pFmt->params.max_keys_per_crypt, MEM_ALIGN_WORD);
 	saved_key = mem_calloc_tiny(sizeof(*saved_key) * pFmt->params.max_keys_per_crypt, MEM_ALIGN_NONE);
-	output = mem_alloc_tiny(sizeof(*output) * pFmt->params.max_keys_per_crypt, MEM_ALIGN_WORD);
 }
 
 static int mschapv2_valid(char *ciphertext, struct fmt_main *pFmt)
@@ -202,13 +197,50 @@ static char *mschapv2_split(char *cipher
   return out;
 }
 
+static void *generate_des_format(uchar* binary)
+{
+  static ARCH_WORD block[6];
+  int chr, src,dst,i;
+  uchar value, mask;
+  ARCH_WORD *ptr;
+
+  memset(block, 0, sizeof(ARCH_WORD) * 6);
+
+  for (chr = 0; chr < 24; chr=chr + 8)
+  {
+  	dst = 0;
+  	for(i=0; i<8; i++)
+  	{
+  		value = binary[chr + i];
+  		mask = 0x80;
+
+  		for (src = 0; src < 8; src++) {
+  			if (value & mask)
+  				block[(chr/4) + (dst>>5)]|= 1 << (dst & 0x1F);
+  			mask >>= 1;
+  			dst++;
+  		}
+  	}
+  }
+
+  /* Apply initial permutation on ciphertext blocks */
+  for(i=0; i<6; i=i+2)
+  {
+	  ptr = (ARCH_WORD *)DES_do_IP(&block[i]);
+	  block[i] = ptr[1];
+	  block[i+1] = ptr[0];
+  }
+
+  return block;
+}
+
 static void *mschapv2_get_binary(char *ciphertext)
 {
-  static uchar *binary;
+  uchar *binary;
   int i;
+  ARCH_WORD *ptr;
 
-  if (!binary) binary = mem_alloc_tiny(BINARY_SIZE, MEM_ALIGN_WORD);
-
+  binary = mem_alloc_tiny(BINARY_SIZE, MEM_ALIGN_WORD);
   ciphertext += 10 + 16*2 + 1; /* Skip - $MSCHAPv2$, Authenticator Challenge */
 
   for (i=0; i<BINARY_SIZE; i++)
@@ -216,32 +248,41 @@ static void *mschapv2_get_binary(char *c
     binary[i] = (atoi16[ARCH_INDEX(ciphertext[i*2])])<<4;
     binary[i] |= (atoi16[ARCH_INDEX(ciphertext[i*2+1])]);
   }
-  return binary;
+
+  /* Set binary in DES format */
+  ptr = generate_des_format(binary);
+  return ptr;
 }
 
-static inline void setup_des_key(unsigned char key_56[], DES_key_schedule *ks)
+
+static inline void setup_des_key(unsigned char key_56[], int index)
 {
-  DES_cblock key;
+  char key[8];
 
-  key[0] = key_56[0];
-  key[1] = (key_56[0] << 7) | (key_56[1] >> 1);
-  key[2] = (key_56[1] << 6) | (key_56[2] >> 2);
-  key[3] = (key_56[2] << 5) | (key_56[3] >> 3);
-  key[4] = (key_56[3] << 4) | (key_56[4] >> 4);
-  key[5] = (key_56[4] << 3) | (key_56[5] >> 5);
-  key[6] = (key_56[5] << 2) | (key_56[6] >> 6);
-  key[7] = (key_56[6] << 1);
+   /* Right shift key bytes by 1 to bring in openssl format */
+   /* Each byte of key is xored with 0x80 to pass check for 0 in DES_bs_set_key() */
 
-  DES_set_key(&key, ks);
+   key[0] = (key_56[0] >> 1) | 0x80;
+   key[1] = (((key_56[0] << 7) | (key_56[1] >> 1)) >>1) | 0x80;
+   key[2] = (((key_56[1] << 6) | (key_56[2] >> 2)) >>1) | 0x80;
+   key[3] = (((key_56[2] << 5) | (key_56[3] >> 3)) >>1) | 0x80;
+   key[4] = (((key_56[3] << 4) | (key_56[4] >> 4)) >>1) | 0x80;
+   key[5] = (((key_56[4] << 3) | (key_56[5] >> 5)) >>1) | 0x80;
+   key[6] = (((key_56[5] << 2) | (key_56[6] >> 6)) >>1) | 0x80;
+   key[7] = ((key_56[6] << 1) >>1 ) | 0x80;
+
+  DES_bs_set_key((char*)key, index);
 }
 
+
 /* Calculate the MSCHAPv2 response for the given challenge, using the
    specified authentication identity (username), password and client
    nonce.
 */
+
 static void mschapv2_crypt_all(int count)
 {
-	DES_key_schedule ks;
+
 	int i;
 
 	if (!keys_prepared) {
@@ -258,54 +299,67 @@ static void mschapv2_crypt_all(int count
 
 			/* NULL-padding the 16-byte hash to 21-bytes is made in cmp_exact if needed */
 		}
-		keys_prepared = 1;
-	}
-
 #ifdef _OPENMP
-#pragma omp parallel for default(none) private(i, ks) shared(count, output, challenge, saved_key)
+#pragma omp parallel for
 #endif
-	for(i=0; i<count; i++) {
+		for(i=0; i<count; i++)
+		    	setup_des_key(saved_key[i], i);
 
-		/* Just do first DES for a partial binary */
-		setup_des_key(saved_key[i], &ks);
-		DES_ecb_encrypt((DES_cblock*)challenge, (DES_cblock*)output[i], &ks, DES_ENCRYPT);
+		keys_prepared = 1;
 	}
+
+	/* Bitsliced des encryption */
+	DES_bs_crypt_plain(count);
+
 }
 
+
 static int mschapv2_cmp_all(void *binary, int count)
 {
-	int index = 0;
-	for(; index<count; index++)
-		if (!memcmp(output[index], binary, PARTIAL_BINARY_SIZE))
-			return 1;
-	return 0;
+	return DES_bs_cmp_all((ARCH_WORD *)binary, count);
 }
 
 static int mschapv2_cmp_one(void *binary, int index)
 {
-	return (!memcmp(output[index], binary, PARTIAL_BINARY_SIZE));
+	return DES_bs_cmp_one((ARCH_WORD *)binary, 32, index);
 }
 
 static int mschapv2_cmp_exact(char *source, int index)
 {
-	DES_key_schedule ks;
-	uchar binary[24];
+
+	ARCH_WORD *binary;
 
 	/* NULL-pad 16-byte NTLM hash to 21-bytes (postponed until now) */
 	memset(&saved_key[index][16], 0, 5);
 
-	/* Split resultant value into three 7-byte thirds
-	   DES-encrypt challenge using each third as a key
-	   Concatenate three 8-byte resulting values to form 24-byte LM response
-	*/
-	setup_des_key(saved_key[index], &ks);
-	DES_ecb_encrypt((DES_cblock*)challenge, (DES_cblock*)binary, &ks, DES_ENCRYPT);
-	setup_des_key(&saved_key[index][7], &ks);
-	DES_ecb_encrypt((DES_cblock*)challenge, (DES_cblock*)&binary[8], &ks, DES_ENCRYPT);
-	setup_des_key(&saved_key[index][14], &ks);
-	DES_ecb_encrypt((DES_cblock*)challenge, (DES_cblock*)&binary[16], &ks, DES_ENCRYPT);
+	binary = mschapv2_get_binary(source);
+	if (!DES_bs_cmp_one(binary, 64, index))
+	{
+		setup_des_key(saved_key[0], 0);
+		return 0;
+	}
+
+	setup_des_key(&saved_key[index][7], 0);
+	DES_bs_crypt_plain(1);
+	binary = mschapv2_get_binary(source);
+	if (!DES_bs_cmp_one(&binary[2], 64, 0))
+	{
+		setup_des_key(saved_key[0], 0);
+		return 0;
+	}
+
+
+	setup_des_key(&saved_key[index][14], 0);
+	DES_bs_crypt_plain(1);
+	binary = mschapv2_get_binary(source);
+	if (!DES_bs_cmp_one(&binary[4], 64, 0))
+	{
+		setup_des_key(saved_key[0], 0);
+		return 0;
+	}
 
-	return !memcmp(binary, mschapv2_get_binary(source), BINARY_SIZE);
+	setup_des_key(saved_key[0], 0);
+	return 1;
 }
 
 /* We're essentially using three salts, but we're going to generate a single value here for later use.
@@ -316,11 +370,15 @@ static void *mschapv2_get_salt(char *cip
   static unsigned char binary_salt[SALT_SIZE];
   static SHA_CTX ctx;
   unsigned char tmp[16];
-  int i;
+  int i, cnt;
+  uchar j;
   char *pos = NULL;
   unsigned char digest[20];
+  unsigned char temp[SALT_SIZE];
 
   memset(binary_salt, 0, SALT_SIZE);
+  memset(temp, 0, SALT_SIZE);
+
   memset(digest, 0, 20);
   SHA1_Init(&ctx);
 
@@ -351,12 +409,32 @@ static void *mschapv2_get_salt(char *cip
   SHA1_Final(digest, &ctx);
   memcpy(binary_salt, digest, SALT_SIZE);
 
+  /* Apply IP to salt */
+  for (i = 0; i < 64; i++) {
+  		cnt = DES_IP[i ^ 0x20];
+  		j = (uchar)((binary_salt[cnt >> 3] >> (7 - (cnt & 7))) & 1);
+  		temp[i/8] |= j << (7 - (i % 8));
+  }
+
+  memcpy(binary_salt, temp, SALT_SIZE);
   return (void*)binary_salt;
 }
 
 static void mschapv2_set_salt(void *salt)
 {
-	challenge = salt;
+	int i;
+	unsigned char j;
+	challenge = salt; 
+
+	/* Set same plaintext for all bit layers */
+	for (i = 0; i < 64; i++) {
+			j = (unsigned char) (challenge[i/8] >> (7-(i%8))) & 0x01;
+			if(j==0)
+				Plaintext[i] = 0;
+			else
+				Plaintext[i] = -1;
+		}
+
 }
 
 static void mschapv2_set_key(char *key, int index)
@@ -378,52 +456,52 @@ static int salt_hash(void *salt)
 
 static int binary_hash_0(void *binary)
 {
-	return *(ARCH_WORD_32 *)binary & 0xF;
+	return *(ARCH_WORD *)binary & 0xF;
 }
 
 static int binary_hash_1(void *binary)
 {
-	return *(ARCH_WORD_32 *)binary & 0xFF;
+	return *(ARCH_WORD *)binary & 0xFF;
 }
 
 static int binary_hash_2(void *binary)
 {
-	return *(ARCH_WORD_32 *)binary & 0xFFF;
+	return *(ARCH_WORD *)binary & 0xFFF;
 }
 
 static int binary_hash_3(void *binary)
 {
-	return *(ARCH_WORD_32 *)binary & 0xFFFF;
+	return *(ARCH_WORD *)binary & 0xFFFF;
 }
 
 static int binary_hash_4(void *binary)
 {
-	return *(ARCH_WORD_32 *)binary & 0xFFFFF;
+	return *(ARCH_WORD *)binary & 0xFFFFF;
 }
 
 static int get_hash_0(int index)
 {
-	return *(ARCH_WORD_32 *)output[index] & 0xF;
+	return DES_bs_get_hash_0(index);
 }
 
 static int get_hash_1(int index)
 {
-	return *(ARCH_WORD_32 *)output[index] & 0xFF;
+	return DES_bs_get_hash_1(index);
 }
 
 static int get_hash_2(int index)
 {
-	return *(ARCH_WORD_32 *)output[index] & 0xFFF;
+	return DES_bs_get_hash_2(index);
 }
 
 static int get_hash_3(int index)
 {
-	return *(ARCH_WORD_32 *)output[index] & 0xFFFF;
+	return DES_bs_get_hash_3(index);
 }
 
 static int get_hash_4(int index)
 {
-	return *(ARCH_WORD_32 *)output[index] & 0xFFFFF;
+	return DES_bs_get_hash_4(index);
 }
 
 struct fmt_main fmt_MSCHAPv2 = {

Powered by blists - more mailing lists

Your e-mail address:

Powered by Openwall GNU/*/Linux - Powered by OpenVZ