From 9d1069286ac07731c5bd3a57ee473d9462299229 Mon Sep 17 00:00:00 2001
From: magnum <magnum>
Date: Wed, 7 Dec 2011 01:54:52 +0100
Subject: [PATCH 37/38] salted-sha1 new faster set_key()

---
 src/salted_sha1_fmt_plug.c |  222 ++++++++++++++++---------------------------
 1 files changed, 83 insertions(+), 139 deletions(-)

diff --git a/src/salted_sha1_fmt_plug.c b/src/salted_sha1_fmt_plug.c
index 32ce6bd..db390c5 100644
--- a/src/salted_sha1_fmt_plug.c
+++ b/src/salted_sha1_fmt_plug.c
@@ -13,6 +13,8 @@
 #include "misc.h"
 #include "formats.h"
 #include "arch.h"
+#include "options.h"
+#include "johnswap.h"
 #ifdef HAVE_MPI
 #include "john-mpi.h"
 #endif
@@ -57,10 +59,7 @@
 #ifdef MMX_COEF
 #define MIN_KEYS_PER_CRYPT		NBKEYS
 #define MAX_KEYS_PER_CRYPT		NBKEYS
-//#define GETPOS(i, index)		( (index&3)*4 + ((i)& (0xffffffff-3) )*MMX_COEF + (3-((i)&3)) + (index>>2)*80*MMX_COEF*4 ) //for endianity conversion
-//#define GETPOS(i, index)		( (index&(MMX_COEF-1))*4 + ((i)&(0xffffffff-3))*MMX_COEF + (3-((i)&(MMX_COEF-1))) + (index>>(MMX_COEF>>1))*80*MMX_COEF*4 ) //for endianity conversion
 #define GETPOS(i, index)		( (index&(MMX_COEF-1))*4 + ((i)&(0xffffffff-3))*MMX_COEF + (3-((i)&3)) + (index>>(MMX_COEF>>1))*80*MMX_COEF*4 ) //for endianity conversion
-//#define GETPOS(i, index)		( (index)*4 + ((i)& (0xffffffff-3) )*MMX_COEF + (3-((i)&3)) )
 #else
 #define MIN_KEYS_PER_CRYPT		1
 #define MAX_KEYS_PER_CRYPT		1
@@ -149,9 +148,7 @@ static void * binary(char *ciphertext) {
 	/* stupid overflows */
 	memset(realcipher, 0, BINARY_SIZE + 9);
 	base64_decode(NSLDAP_MAGIC_LENGTH+ciphertext, strlen(ciphertext)-NSLDAP_MAGIC_LENGTH, realcipher);
-// if we try to unify the rawsha1_cmp_one() and and rawsha1_cmp_all() functions, we should endian alter for all MMX_COEF.
-// Right now, for MMX/SSE non-para, we keep the older methods.
-#ifdef SHA1_SSE_PARA
+#ifdef MMX_COEF
 	alter_endianity((unsigned char *)realcipher, BINARY_SIZE);
 #endif
 	return (void *)realcipher;
@@ -165,43 +162,54 @@ static int valid(char *ciphertext, struct fmt_main *pFmt)
 	return !strncasecmp(ciphertext, NSLDAP_MAGIC, NSLDAP_MAGIC_LENGTH);
 }
 
-static int binary_hash_0(void * binary) { return ((ARCH_WORD_32 *)binary)[0] & 0xf; }
-static int binary_hash_1(void * binary) { return ((ARCH_WORD_32 *)binary)[0] & 0xff; }
-static int binary_hash_2(void * binary) { return ((ARCH_WORD_32 *)binary)[0] & 0xfff; }
-static int binary_hash_3(void * binary) { return ((ARCH_WORD_32 *)binary)[0] & 0xffff; }
-static int binary_hash_4(void * binary) { return ((ARCH_WORD_32 *)binary)[0] & 0xfffff; }
-static int binary_hash_5(void * binary) { return ((ARCH_WORD_32 *)binary)[0] & 0xffffff; }
-static int binary_hash_6(void * binary) { return ((ARCH_WORD_32 *)binary)[0] & 0x7ffffff; }
-
-static void set_key(char *key, int index) {
+static void set_key(char *_key, int index)
+{
 #ifdef MMX_COEF
-	int len;
-	int i;
+	const ARCH_WORD_32 *key = (ARCH_WORD_32*)_key;
+	ARCH_WORD_32 *keybuffer = (ARCH_WORD_32*)&saved_key[GETPOS(3, index)];
+	ARCH_WORD_32 *keybuf_word = keybuffer;
+	unsigned int len;
+	ARCH_WORD_32 temp;
 
 	if(index==0)
-	{
 		memset(saved_len, 0, sizeof(saved_len));
-		//memset(saved_key, 0, sizeof(saved_key));
-		i = 0;
-#ifdef SHA1_SSE_PARA
-		for (; i < SHA1_SSE_PARA; ++i)
-#endif
-			memset(&saved_key[i*4*80*MMX_COEF], 0, 56*MMX_COEF);
-	}
-	len = strlen(key);
-	if(len>PLAINTEXT_LENGTH)
-		len = PLAINTEXT_LENGTH;
-	saved_len[index] = len;
-
-	for(i=0;i<len;i++)
-		saved_key[GETPOS(i, index)] = key[i];
 
+	len = 0;
+	while((temp = JOHNSWAP(*key++)) & 0xff000000) {
+		if (!(temp & 0xff0000))
+		{
+			*keybuf_word = (temp & 0xff000000) | 0x800000;
+			len++;
+			goto key_cleaning;
+		}
+		if (!(temp & 0xff00))
+		{
+			*keybuf_word = (temp & 0xffff0000) | 0x8000;
+			len+=2;
+			goto key_cleaning;
+		}
+		if (!(temp & 0xff))
+		{
+			*keybuf_word = temp | 0x80;
+			len+=3;
+			goto key_cleaning;
+		}
+		*keybuf_word = temp;
+		len += 4;
+		keybuf_word += MMX_COEF;
+	}
+	*keybuf_word = 0x80000000;
 
-	saved_key[GETPOS(i, index)] = 0x80;
-	((unsigned int *)saved_key)[15*MMX_COEF + (index&3) + (index>>2)*80*MMX_COEF] = len<<3;
+key_cleaning:
+	keybuf_word += MMX_COEF;
+	while(*keybuf_word) {
+		*keybuf_word = 0;
+		keybuf_word += MMX_COEF;
+	}
 
+	saved_len[index] = len;
 #else
-	strnzcpy(saved_key, key, PLAINTEXT_LENGTH+1);
+	strnzcpy(saved_key, _key, PLAINTEXT_LENGTH + 1);
 #endif
 }
 
@@ -256,7 +264,6 @@ static char *get_key(int index) {
 
 static int cmp_all(void *binary, int count) {
 #ifdef MMX_COEF
-# ifdef SHA1_SSE_PARA
 	unsigned int x,y=0;
 
 #ifdef SHA1_SSE_PARA
@@ -264,75 +271,45 @@ static int cmp_all(void *binary, int count) {
 #endif
 	for(x=0;x<MMX_COEF;x++)
 	{
-		if( ((unsigned int *)binary)[0] == ((unsigned int *)crypt_key)[x+y*MMX_COEF*5] )
+		if( ((ARCH_WORD_32*)binary)[0] ==
+		    ((ARCH_WORD_32*)crypt_key)[x+y*MMX_COEF*5] )
 			return 1;
 	}
 	return 0;
-# else
-	int i=0;
-	while(i< (BINARY_SIZE/4) )
-	{
-		if (
-			( ((unsigned long *)binary)[i] != ((unsigned long *)crypt_key)[i*MMX_COEF])
-			&& ( ((unsigned long *)binary)[i] != ((unsigned long *)crypt_key)[i*MMX_COEF+1])
-#   if (MMX_COEF > 3)
-			&& ( ((unsigned long *)binary)[i] != ((unsigned long *)crypt_key)[i*MMX_COEF+2])
-			&& ( ((unsigned long *)binary)[i] != ((unsigned long *)crypt_key)[i*MMX_COEF+3])
-#   endif
-		)
-			return 0;
-		i++;
-	}
-	return 1;
-# endif
 #else
 	return !memcmp(binary, crypt_key, BINARY_SIZE);
 #endif
 }
 
 static int cmp_exact(char *source, int count){
-  return (1);
+	return (1);
 }
 
 static int cmp_one(void * binary, int index)
 {
 #ifdef MMX_COEF
-# if SHA1_SSE_PARA
 	unsigned int x,y;
-        x = index&3;
-        y = index/4;
-
-        if( ((unsigned int *)binary)[0] != ((unsigned int *)crypt_key)[x+y*MMX_COEF*5] )
-                return 0;
-        if( ((unsigned int *)binary)[1] != ((unsigned int *)crypt_key)[x+y*MMX_COEF*5+4] )
-                return 0;
-        if( ((unsigned int *)binary)[2] != ((unsigned int *)crypt_key)[x+y*MMX_COEF*5+8] )
-                return 0;
-        if( ((unsigned int *)binary)[3] != ((unsigned int *)crypt_key)[x+y*MMX_COEF*5+12] )
-                return 0;
-        if( ((unsigned int *)binary)[4] != ((unsigned int *)crypt_key)[x+y*MMX_COEF*5+16] )
-                return 0;
-        return 1;
-# else
-	int i = 0;
-	for(i=0;i<(BINARY_SIZE/4);i++)
-		if ( ((unsigned long *)binary)[i] != ((unsigned long *)crypt_key)[i*MMX_COEF+index] )
-			return 0;
+	x = index&3;
+	y = index/4;
+
+	if( ((ARCH_WORD_32*)binary)[0] != ((ARCH_WORD_32*)crypt_key)[x+y*MMX_COEF*5] )
+		return 0;
+	if( ((ARCH_WORD_32*)binary)[1] != ((ARCH_WORD_32*)crypt_key)[x+y*MMX_COEF*5+MMX_COEF*1] )
+		return 0;
+	if( ((ARCH_WORD_32*)binary)[2] != ((ARCH_WORD_32*)crypt_key)[x+y*MMX_COEF*5+MMX_COEF*2] )
+		return 0;
+	if( ((ARCH_WORD_32*)binary)[3] != ((ARCH_WORD_32*)crypt_key)[x+y*MMX_COEF*5+MMX_COEF*3] )
+		return 0;
+	if( ((ARCH_WORD_32*)binary)[4] != ((ARCH_WORD_32*)crypt_key)[x+y*MMX_COEF*5+MMX_COEF*4] )
+		return 0;
 	return 1;
-# endif
 #else
 	return cmp_all(binary, index);
 #endif
 }
 
-
 static void set_salt(void *salt) {
 	memcpy(&saved_salt, salt, sizeof(struct s_salt));
-/*
-data is not a pointer, so this check did not make sense
-	if(saved_salt.data == NULL)
-		printf("data is NULL\n");
-*/
 }
 
 #ifdef MMX_COEF
@@ -359,7 +336,7 @@ static void crypt_all(int count) {
 # if SHA1_SSE_PARA
 	SSESHA1body(saved_key, (unsigned int *)crypt_key, NULL, 0);
 # else
-	shammx_nosizeupdate((unsigned char *) crypt_key, (unsigned char *) saved_key, 1);
+	shammx_nosizeupdate_nofinalbyteswap((unsigned char *) crypt_key, (unsigned char *) saved_key, 1);
 
 # endif
 
@@ -374,64 +351,31 @@ static void crypt_all(int count) {
 
 }
 
+static int binary_hash_0(void * binary) { return ((ARCH_WORD_32*)binary)[0] & 0xf; }
+static int binary_hash_1(void * binary) { return ((ARCH_WORD_32*)binary)[0] & 0xff; }
+static int binary_hash_2(void * binary) { return ((ARCH_WORD_32*)binary)[0] & 0xfff; }
+static int binary_hash_3(void * binary) { return ((ARCH_WORD_32*)binary)[0] & 0xffff; }
+static int binary_hash_4(void * binary) { return ((ARCH_WORD_32*)binary)[0] & 0xfffff; }
+static int binary_hash_5(void * binary) { return ((ARCH_WORD_32*)binary)[0] & 0xffffff; }
+static int binary_hash_6(void * binary) { return ((ARCH_WORD_32*)binary)[0] & 0x7ffffff; }
+
 #ifdef MMX_COEF
-static int get_hash_0(int index)
-{
-	unsigned int x,y;
-        x = index&3;
-        y = index/4;
-	return ((unsigned int *)crypt_key)[x+y*MMX_COEF*5] & 0xf;
-}
-static int get_hash_1(int index)
-{
-	unsigned int x,y;
-        x = index&3;
-        y = index/4;
-	return ((unsigned int *)crypt_key)[x+y*MMX_COEF*5] & 0xff;
-}
-static int get_hash_2(int index)
-{
-	unsigned int x,y;
-        x = index&3;
-        y = index/4;
-	return ((unsigned int *)crypt_key)[x+y*MMX_COEF*5] & 0xfff;
-}
-static int get_hash_3(int index)
-{
-	unsigned int x,y;
-        x = index&3;
-        y = index/4;
-	return ((unsigned int *)crypt_key)[x+y*MMX_COEF*5] & 0xffff;
-}
-static int get_hash_4(int index)
-{
-	unsigned int x,y;
-        x = index&3;
-        y = index/4;
-	return ((unsigned int *)crypt_key)[x+y*MMX_COEF*5] & 0xfffff;
-}
-static int get_hash_5(int index)
-{
-	unsigned int x,y;
-        x = index&3;
-        y = index/4;
-	return ((unsigned int *)crypt_key)[x+y*MMX_COEF*5] & 0xffffff;
-}
-static int get_hash_6(int index)
-{
-	unsigned int x,y;
-        x = index&3;
-        y = index/4;
-	return ((unsigned int *)crypt_key)[x+y*MMX_COEF*5] & 0x7ffffff;
-}
+#define HASH_IDX ((index&3)+(index/4)*MMX_COEF*5)
+static int get_hash_0(int index) { return ((ARCH_WORD_32*)crypt_key)[HASH_IDX] & 0xf; }
+static int get_hash_1(int index) { return ((ARCH_WORD_32*)crypt_key)[HASH_IDX] & 0xff; }
+static int get_hash_2(int index) { return ((ARCH_WORD_32*)crypt_key)[HASH_IDX] & 0xfff; }
+static int get_hash_3(int index) { return ((ARCH_WORD_32*)crypt_key)[HASH_IDX] & 0xffff; }
+static int get_hash_4(int index) { return ((ARCH_WORD_32*)crypt_key)[HASH_IDX] & 0xfffff; }
+static int get_hash_5(int index) { return ((ARCH_WORD_32*)crypt_key)[HASH_IDX] & 0xffffff; }
+static int get_hash_6(int index) { return ((ARCH_WORD_32*)crypt_key)[HASH_IDX] & 0x7ffffff; }
 #else
-static int get_hash_0(int index) { return ((unsigned int *)crypt_key)[0] & 0xf; }
-static int get_hash_1(int index) { return ((unsigned int *)crypt_key)[0] & 0xff; }
-static int get_hash_2(int index) { return ((unsigned int *)crypt_key)[0] & 0xfff; }
-static int get_hash_3(int index) { return ((unsigned int *)crypt_key)[0] & 0xffff; }
-static int get_hash_4(int index) { return ((unsigned int *)crypt_key)[0] & 0xfffff; }
-static int get_hash_5(int index) { return ((unsigned int *)crypt_key)[0] & 0xffffff; }
-static int get_hash_6(int index) { return ((unsigned int *)crypt_key)[0] & 0x7ffffff; }
+static int get_hash_0(int index) { return ((ARCH_WORD_32*)crypt_key)[0] & 0xf; }
+static int get_hash_1(int index) { return ((ARCH_WORD_32*)crypt_key)[0] & 0xff; }
+static int get_hash_2(int index) { return ((ARCH_WORD_32*)crypt_key)[0] & 0xfff; }
+static int get_hash_3(int index) { return ((ARCH_WORD_32*)crypt_key)[0] & 0xffff; }
+static int get_hash_4(int index) { return ((ARCH_WORD_32*)crypt_key)[0] & 0xfffff; }
+static int get_hash_5(int index) { return ((ARCH_WORD_32*)crypt_key)[0] & 0xffffff; }
+static int get_hash_6(int index) { return ((ARCH_WORD_32*)crypt_key)[0] & 0x7ffffff; }
 #endif
 
 static int salt_hash(void *salt)
-- 
1.7.5.4

