Openwall GNU/*/Linux - a small security-enhanced Linux distro for servers
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Tue, 24 Jan 2012 21:53:19 +0530
From: Piyush Mittal <piyush.cse29@...il.com>
To: john-dev@...ts.openwall.com
Subject: Re: Bit slice implementation of DES based hashes

Here I am enclosing my code, please check whether key initialisation is
done right or not? Also please ignore commented lines of code.

Thanks,
-- 
Piyush Mittal

[ CONTENT OF TYPE text/html SKIPPED ]

/*
 * Copyright (c) 2004 Simon Marechal
 * simon.marechal at thales-security.com
 *
 * UTF-8 support by magnum 2011, no rights reserved
 */

#include <string.h>
#include <openssl/des.h>
#include "arch.h"
#include "misc.h"
#include "common.h"
#include "formats.h"
#include "unicode.h"
#include "DES_bs.h"

#define FORMAT_LABEL			"oracleee"
#define FORMAT_NAME			"Oracleee"
#define ALGORITHM_NAME			"oracleee"

#define BENCHMARK_COMMENT		""
#define BENCHMARK_LENGTH		-1

#define PLAINTEXT_LENGTH		120 // worst case UTF-8 is 40 characters of Unicode, that'll do

#define BINARY_SIZE			8
#define SALT_SIZE			(32 + 4)  // also contain the NULL
#define CIPHERTEXT_LENGTH		16

#define MIN_KEYS_PER_CRYPT		DES_BS_DEPTH
#define MAX_KEYS_PER_CRYPT		DES_BS_DEPTH


#if DES_BS_VECTOR
#define DEPTH				[depth]
#define START				[0]
#define init_depth() \
	int depth; \
	depth = index >> ARCH_BITS_LOG; \
	index &= (ARCH_BITS - 1);
#define for_each_depth() \
	for (depth = 0; depth < DES_BS_VECTOR; depth++)
#else
#define DEPTH
#define START
#define init_depth()
#define for_each_depth()
#endif



static 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,
	63, 55, 47, 39, 31, 23, 15, 7,
	56, 48, 40, 32, 24, 16, 8, 0,
	58, 50, 42, 34, 26, 18, 10, 2,
	60, 52, 44, 36, 28, 20, 12, 4,
	62, 54, 46, 38, 30, 22, 14, 6
};



int out1[64];//new modification
unsigned long outtemp2[128];


long *oracle_binary(char *ciphertext);
static struct fmt_tests oracle_tests[] = {

	{"O$SYS#0170453E89F8CDA7", "H4X0R"},
	//{"O$SYS#0170453E89F8CDA7", "H4X0R"},//C648972D2BE43FA4
	//{"O$SIMON#C4EB3152E17F24A4", "TST" },
	//{"O$BOB#b02c8e79ed2e7f46", "LAPIN" },
	
	//{"4F8BC1809CB2AF77", "A",             {"SIMON"} },
	//{"C4EB3152E17F24A4", "TST",           {"SIMON"} },
	//{"b02c8e79ed2e7f46", "LAPIN",         {"BOB"} },
	{NULL}

};

#if ARCH_LITTLE_ENDIAN
#define ENDIAN_SHIFT_L  << 8
#define ENDIAN_SHIFT_R  >> 8
#else
#define ENDIAN_SHIFT_L
#define ENDIAN_SHIFT_R
#endif

static int salt_length;
static int key_length;
static char *plain_key;

 static char temp_salt[9];
 static char temp_key[9];
static int valid(char *ciphertext, struct fmt_main *pFmt)
{
	int i;
	int l;

	/*
	 * 2 cases
	 * 1 - it comes from the disk, and does not have O$ + salt
	 * 2 - it comes from memory, and has got O$ + salt + # + blah
	 */

	if (!memcmp(ciphertext, "O$", 2))
	{
		l = strlen(ciphertext) - CIPHERTEXT_LENGTH;
		if(ciphertext[l-1]!='#')
			return 0;
	}
	else
	{
		if(strlen(ciphertext)!=CIPHERTEXT_LENGTH)
			return 0;
		l = 0;
	}
	for (i = l; i < l + CIPHERTEXT_LENGTH; i++){
		if (!(  (('0' <= ciphertext[i])&&(ciphertext[i] <= '9')) ||
			(('a' <= ciphertext[i])&&(ciphertext[i] <= 'f'))
			|| (('A' <= ciphertext[i])&&(ciphertext[i] <= 'F'))))
			return 0;
	}

	return 1;
}

static char *prepare(char *split_fields[10], struct fmt_main *pFmt)
{
	char *cp;

	if (!strncmp(split_fields[1], "O$", 2))
		return split_fields[1];
	cp = mem_alloc(strlen(split_fields[0]) + strlen(split_fields[1]) + 4);
	sprintf (cp, "O$%s#%s", split_fields[0], split_fields[1]);
	if (valid(cp, pFmt))
	{
		UTF8 tmp8[16*3+1];
		UTF16 tmp16[17];
		int utf8len, utf16len;

		// we no longer need this.  It was just used for valid().   We will recompute
		// all lengths, after we do an upcase, since upcase can change the length of the
		// utf8 string.
		MEM_FREE(cp);

		// Upcase user name, --encoding aware
		utf8len = enc_uc(tmp8, 16*3, (unsigned char*)split_fields[0], strlen(split_fields[0]));

		if (utf8len <= 0 && split_fields[0][0])
			return split_fields[1];

		// make sure this 'fits' into 16 unicode's
		utf16len = enc_to_utf16(tmp16, 16, tmp8, utf8len);
		if (utf16len <= 0)
			return split_fields[1];

		cp = mem_alloc_tiny(utf8len + strlen(split_fields[1]) + 4, MEM_ALIGN_NONE);
		sprintf (cp, "O$%s#%s", tmp8, split_fields[1]);
#ifdef DEBUG_ORACLE
		printf ("tmp8         : %s\n", tmp8);
#endif
		return cp;
	}
	MEM_FREE(cp);
	return split_fields[1];
}

static void oracle_init(struct fmt_main *pFmt)
{
        DES_bs_init(2);
}


static void oracle_set_key(char *key, int index) {
int l;


	plain_key = key;



//10101011011101000010000011000010011001101111001101101110


for(l=0;l<strlen(key);l++)
temp_key[l]=key[l];


//key="\x80\x11\x22\x33\x44\x55\x66\x77";
//key="\xAB\x74\x20\xC2\x66\xF3\x6E";
key="\xAA\xBB\x09\x18\x27\x36\xCC\xDD";

//key="\x01\x23\x45\x67\x89\xAB\xCD\xEF";
DES_bs_set_key(key,index);


//for_each_depth(){
//bnew = (DES_bs_vector *)&DES_bs_all.K[0]bd;
//for (bit = 0; bit<56; bit++)
//bnew[bit]bd = (binary_plain_salt_mix[bit]);
//}



//00000000010001010001001100111000100101010111001101110111



//75948856013687




}


long *oracle_binary(char *ciphertext)
{
unsigned long out;  
unsigned long outtemp1[64];


	int i,j,l = strlen(ciphertext),s=0;;
	 out=(ARCH_INDEX(ciphertext[0])&0xff);
        for(i=1;i<l;i++)
	{         
		out =(out<<8)|(ARCH_INDEX(ciphertext[i])&0xff);
			
	}
//110011001000100001000110000000
//{83, 73, 77, 79, 78, 65, 0, 0, 0}
//01001111 01001101 01001001 01010011

for(i=0;i<l*8;i++){
   outtemp1[i]=(out>>i)&1;
   }


for(j=0;j<64-(l*8);j++)
out1[j]=0;

for(i=(l*8)-1;i>=0;i--,j++)
out1[j]=outtemp1[i];

l=0;
j=0;

  for(i=0;i<128;i++){
    if(i==8||i==24||i==40||i==56||i==72||i==88||i==104||i==120){
              for(j=0+s;j<8+s;j++,i++)
      //printf("%d..%d\n",i,j);
                outtemp2[i]=out1[j];

     }      
    outtemp2[i]=0;
 s=j;
}        
//        printf("new value\n");
  //            for(j=0;j<128;j++)
    //          printf("%d",outtemp2[j]);


return outtemp2;

}


static char *oracle_get_key(int index) {
	static UTF8 UC_Key[PLAINTEXT_LENGTH*3*3+1];
	// Calling this will ONLY upcase characters 'valid' in the code page. There are MANY
	// code pages which mssql WILL upcase the letter (in UCS-2), but there is no upper case value
	// in the code page.  Thus we MUST keep the lower cased letter in this case.
	enc_uc(UC_Key, PLAINTEXT_LENGTH*3*3, (UTF8*)plain_key, strlen(plain_key));
	return (char*)UC_Key;
}


static void oracle_crypt_all(int count)
{
   int index=count;
   int ofs,j,length,l,bit,i,dst;
   long *binary_plain_salt_mix;
   DES_bs_vector *b=NULL;
   unsigned long finalresult;
   long cryptkey1[64];
   long cryptkey2[64];
   int result[64];

   int temp1[64];
   char *key;

   init_depth();
   
strcat(temp_salt,temp_key);

binary_plain_salt_mix=oracle_binary(temp_salt);


for(i=0;i<64;i++)
 cryptkey1[i]=binary_plain_salt_mix[i];

i=0;

for(j=64;j<128;j++,i++)
 cryptkey2[i]=binary_plain_salt_mix[j];


for(ofs=0;ofs<9;ofs++)
temp_salt[ofs]=NULL;


DES_bs_crypt_Oracle(cryptkey1);
binary_plain_salt_mix=NULL;

/*
b = (DES_bs_vector *)&DES_bs_all.B[0] DEPTH;


for (bit =0; bit<64; bit++)
        temp1[bit]=(b[bit]START & 1);
     

for (dst = 0; dst < 64; dst++) {
		int src = DES_IP[dst ^ 0x20];
	temp1[dst]=temp1[src];
}


printf("Temp value..\n");
for(i=0;i<64;i++)
    printf("%d",temp1[i]);
printf("\n");
*/

//0000000101110000010001010011111010001001111110001100110110100111//30 1

//0010010100111000010100100110001011111010001101000100111111001010


//1111110001000001001010011000001000000101001101101001101011011111


/*

     for (bit =0; bit<64; bit++)
        temp1[bit]=temp1[bit]^cryptkey2[bit];
     
      DES_bs_clear_keys_LM();
    
      key="\x01\x23\x45\x67\x89\xAB\xCD\xEF";

      DES_bs_set_key_LM(key,index);

 DES_bs_crypt_Oracle(temp1);



b = (DES_bs_vector *)&DES_bs_all.B[0] DEPTH;

for (bit =63; bit>=0; bit--, b++){
	result[bit]= ((b[0]START>>index) & 1);
}
finalresult=(result[63]<<1)|result[62];


for (bit =61; bit>=0; bit--)
 finalresult=(finalresult<<1)|result[bit];

//printf("Final result...%ld\n",finalresult);

for (bit = 0; bit<64; bit++)
result[bit]=0;





key=decimal_to_text(finalresult);
finalresult=0;
DES_bs_clear_keys_LM();
DES_bs_set_key_LM(key,index);

DES_bs_crypt_Oracle(cryptkey1);
b = (DES_bs_vector *)&DES_bs_all.B[0] DEPTH;

     for (bit =0; bit<64; bit++)
        temp1[bit]=(b[bit]START & 1)^cryptkey2[bit];
     
      DES_bs_clear_keys_LM();
    
      DES_bs_set_key_LM(key,index);

 DES_bs_crypt_Oracle(cryptkey2);




*/





}

static void * oracle_get_salt(char * ciphertext)
{
        char salt[8];
	int l,i;
        l=2;
      for(i=0;i<9;i++)
        temp_salt[i]=NULL;
	while( ciphertext[l] && (ciphertext[l]!='#') )
	{ 
		salt[l-2] = ciphertext[l];
		l++;
		if (l-2 >= SALT_SIZE-2) break;
	}
	salt[l-2] = 0;
for(l=0;l<strlen(salt);l++)
temp_salt[l]=salt[l];

//ciphertext="";
	return ciphertext;
}


static int binary_hash_0(void *binary)
{


printf("Binary Value 0....%ld\n",*(long *)binary);
//00000111000101100000010000011000
//00000000011101001111011110011000
printf("Binary Value 1....%ld\n",(long *)binary+1);

//int temp;
//temp=2720165088 & 0xF;
//binary[0]=2720165088;
	return *(ARCH_WORD *)binary & 0xF;
//return  temp;

}

static int binary_hash_1(void *binary)
{
//int temp;
//temp=2720165088 & 0xFF;
//binary[0]=2720165088;
	return *(ARCH_WORD *)binary & 0xFF;
//return  temp;


}

static int binary_hash_2(void *binary)
{
//int temp;
//temp=2720165088 & 0xFFF;
//binary[0]=2720165088;
	return *(ARCH_WORD *)binary & 0xFFF;
//return  temp;
}

static int binary_hash_3(void *binary)
{ 
//int temp;
//temp=2720165088 & 0xFFFF;
//binary[0]=2720165088;
	return *(ARCH_WORD *)binary & 0xFFFF;
//return  temp;


	//return *(ARCH_WORD *)binary & 0xFFFF;
}

static int binary_hash_4(void *binary)
{
//int temp;
//temp=2720165088 & 0xFFFFF;
//binary[0]=2720165088;
	return *(ARCH_WORD *)binary & 0xFFFFF;
//return  temp;



//	return *(ARCH_WORD *)binary & 0xFFFFF;
}

static int get_hash_0(int index)
{
	return DES_bs_get_hash(index, 4);
}

static int get_hash_1(int index)
{
	return DES_bs_get_hash(index, 8);
}

static int get_hash_2(int index)
{
	return DES_bs_get_hash(index, 12);
}

static int get_hash_3(int index)
{
	return DES_bs_get_hash(index, 16);
}

static int get_hash_4(int index)
{
	return DES_bs_get_hash(index, 20);
}


static int cmp_all(void *binary, int count)
{long *temp[2];
temp[0]=2720165088;
//binary[0]=2720165088;
	//return *(ARCH_WORD *)binary & 0xF;
return  DES_bs_cmp_all(temp);

	//return DES_bs_cmp_all((ARCH_WORD *)binary);
}

static int cmp_one(void *binary, int index)
{
long *temp[2];
temp[0]=2720165088;
temp[1]=2257408064;

	return DES_bs_cmp_one(temp, 32, index);//(ARCH_WORD *)binary
}

static int cmp_exact(char *source, int index)
{
long *temp[2];
temp[0]=2720165088;
temp[1]=2257408064;
	return DES_bs_cmp_one_oracle(temp, 64, index);//(ARCH_WORD *)binary
}


struct fmt_main fmt_oracle = {
	{
		FORMAT_LABEL,
		FORMAT_NAME,
		ALGORITHM_NAME,
		BENCHMARK_COMMENT,
		BENCHMARK_LENGTH,
		PLAINTEXT_LENGTH,
		BINARY_SIZE,
		SALT_SIZE,
		MIN_KEYS_PER_CRYPT,
		MAX_KEYS_PER_CRYPT,
		FMT_8_BIT | FMT_UNICODE | FMT_UTF8,
		oracle_tests
	}, {
		oracle_init,
		prepare,
		valid,
		fmt_default_split,
		DES_bs_get_binary_LM,
		oracle_get_salt,
		{
			binary_hash_0,
			binary_hash_1,
			binary_hash_2,
			binary_hash_3,
			binary_hash_4
		},
		fmt_default_salt_hash,
		fmt_default_set_salt,
		oracle_set_key,
		oracle_get_key,
		fmt_default_clear_keys,
		oracle_crypt_all,
		{
			get_hash_0,
			get_hash_1,
			get_hash_2,
			get_hash_3,
			get_hash_4
		},
		cmp_all,
		cmp_one,
		cmp_exact
	}
};

/*
 * This file is part of John the Ripper password cracker,
 * Copyright (c) 1996-2001,2003,2010,2011 by Solar Designer
 */

#include "arch.h"
#include "DES_std.h"


//unsigned int t=1;

#if !DES_BS_ASM
#include "DES_bs.h"

#if defined(__ALTIVEC__) && DES_BS_DEPTH == 128
#undef DES_BS_VECTOR

#ifdef __linux__
#include <altivec.h>
#endif

typedef vector signed int vtype;

#define vst(dst, ofs, src) \
	vec_st((src), (ofs) * sizeof(DES_bs_vector), &(dst))

#define vxorf(a, b) \
	vec_xor((a), (b))

#define vnot(dst, a) \
	(dst) = vec_nor((a), (a))
#define vand(dst, a, b) \
	(dst) = vec_and((a), (b))
#define vor(dst, a, b) \
	(dst) = vec_or((a), (b))
#define vandn(dst, a, b) \
	(dst) = vec_andc((a), (b))
#define vxorn(dst, a, b) \
	(dst) = vec_xor((a), (b)); \
	(dst) = vec_nor((dst), (dst))
#define vnor(dst, a, b) \
	(dst) = vec_nor((a), (b))
#define vsel(dst, a, b, c) \
	(dst) = vec_sel((a), (b), (c))

#elif defined(__ALTIVEC__) && \
    ((ARCH_BITS == 64 && DES_BS_DEPTH == 192) || \
    (ARCH_BITS == 32 && DES_BS_DEPTH == 160))
#undef DES_BS_VECTOR

#ifdef __linux__
#include <altivec.h>
#endif

typedef struct {
	vector signed int f;
	ARCH_WORD g;
} vtype;

#define vst(dst, ofs, src) \
	vec_st((src).f, (ofs) * sizeof(DES_bs_vector), &((vtype *)&(dst))->f); \
	((vtype *)((DES_bs_vector *)&(dst) + (ofs)))->g = (src).g

#define vxor(dst, a, b) \
	(dst).f = vec_xor((a).f, (b).f); \
	(dst).g = (a).g ^ (b).g

#define vnot(dst, a) \
	(dst).f = vec_nor((a).f, (a).f); \
	(dst).g = ~(a).g
#define vand(dst, a, b) \
	(dst).f = vec_and((a).f, (b).f); \
	(dst).g = (a).g & (b).g
#define vor(dst, a, b) \
	(dst).f = vec_or((a).f, (b).f); \
	(dst).g = (a).g | (b).g
#define vandn(dst, a, b) \
	(dst).f = vec_andc((a).f, (b).f); \
	(dst).g = (a).g & ~(b).g
#define vxorn(dst, a, b) \
	(dst).f = vec_xor((a).f, (b).f); \
	(dst).f = vec_nor((dst).f, (dst).f); \
	(dst).g = ~((a).g ^ (b).g)
#define vnor(dst, a, b) \
	(dst).f = vec_nor((a).f, (b).f); \
	(dst).g = ~((a).g | (b).g)
#define vsel(dst, a, b, c) \
	(dst).f = vec_sel((a).f, (b).f, (c).f); \
	(dst).g = (((a).g & ~(c).g) ^ ((b).g & (c).g))

#elif defined(__ALTIVEC__) && DES_BS_DEPTH == 256
#undef DES_BS_VECTOR

#ifdef __linux__
#include <altivec.h>
#endif

typedef struct {
	vector signed int f, g;
} vtype;

#define vst(dst, ofs, src) \
	vec_st((src).f, (ofs) * sizeof(DES_bs_vector), &((vtype *)&(dst))->f); \
	vec_st((src).g, (ofs) * sizeof(DES_bs_vector), &((vtype *)&(dst))->g)

#define vxor(dst, a, b) \
	(dst).f = vec_xor((a).f, (b).f); \
	(dst).g = vec_xor((a).g, (b).g)

#define vnot(dst, a) \
	(dst).f = vec_nor((a).f, (a).f); \
	(dst).g = vec_nor((a).g, (a).g)
#define vand(dst, a, b) \
	(dst).f = vec_and((a).f, (b).f); \
	(dst).g = vec_and((a).g, (b).g)
#define vor(dst, a, b) \
	(dst).f = vec_or((a).f, (b).f); \
	(dst).g = vec_or((a).g, (b).g)
#define vandn(dst, a, b) \
	(dst).f = vec_andc((a).f, (b).f); \
	(dst).g = vec_andc((a).g, (b).g)
#define vxorn(dst, a, b) \
	(dst).f = vec_xor((a).f, (b).f); \
	(dst).g = vec_xor((a).g, (b).g); \
	(dst).f = vec_nor((dst).f, (dst).f); \
	(dst).g = vec_nor((dst).g, (dst).g)
#define vnor(dst, a, b) \
	(dst).f = vec_nor((a).f, (b).f); \
	(dst).g = vec_nor((a).g, (b).g)
#define vsel(dst, a, b, c) \
	(dst).f = vec_sel((a).f, (b).f, (c).f); \
	(dst).g = vec_sel((a).g, (b).g, (c).g)

#elif defined(__AVX__) && DES_BS_DEPTH == 256 && !defined(DES_BS_NO_AVX256)
#undef DES_BS_VECTOR

#include <immintrin.h>

/* Not __m256i because bitwise ops are "floating-point" with AVX */
typedef __m256 vtype;

#define vst(dst, ofs, src) \
	_mm256_store_ps((float *)((DES_bs_vector *)&(dst) + (ofs)), (src))

#define vxorf(a, b) \
	_mm256_xor_ps((a), (b))

#define vnot(dst, a) \
	(dst) = _mm256_xor_ps((a), *(vtype *)&DES_bs_all.ones)
#define vand(dst, a, b) \
	(dst) = _mm256_and_ps((a), (b))
#define vor(dst, a, b) \
	(dst) = _mm256_or_ps((a), (b))
#define vandn(dst, a, b) \
	(dst) = _mm256_andnot_ps((b), (a))
#define vxorn(dst, a, b) \
	(dst) = _mm256_xor_ps(_mm256_xor_ps((a), (b)), \
	    *(vtype *)&DES_bs_all.ones)

#ifdef __XOP__
#define vnor(dst, a, b) \
	(dst) = _mm256_xor_ps(_mm256_or_ps((a), (b)), \
	    *(vtype *)&DES_bs_all.ones)
/* This could be _mm256_cmov_ps(), but it does not exist (yet?) */
#define vsel(dst, a, b, c) \
	(dst) = __builtin_ia32_vpcmov_v8sf256((b), (a), (c))
#endif

#elif defined(__AVX__) && DES_BS_DEPTH == 384 && !defined(DES_BS_NO_AVX128)
#undef DES_BS_VECTOR

#include <immintrin.h>
#ifdef __XOP__
#include <x86intrin.h>
#endif

typedef struct {
/* Not __m256i because bitwise ops are "floating-point" with AVX */
	__m256 f;
	__m128i g;
} vtype;

#define vst(dst, ofs, src) \
	_mm256_store_ps( \
	    (float *)&((vtype *)((DES_bs_vector *)&(dst) + (ofs)))->f, \
	    (src).f); \
	_mm_store_si128(&((vtype *)((DES_bs_vector *)&(dst) + (ofs)))->g, \
	    (src).g)

#define vxor(dst, a, b) \
	(dst).f = _mm256_xor_ps((a).f, (b).f); \
	(dst).g = _mm_xor_si128((a).g, (b).g)

#define vnot(dst, a) \
	(dst).f = _mm256_xor_ps((a).f, ((vtype *)&DES_bs_all.ones)->f); \
	(dst).g = _mm_xor_si128((a).g, ((vtype *)&DES_bs_all.ones)->g)
#define vand(dst, a, b) \
	(dst).f = _mm256_and_ps((a).f, (b).f); \
	(dst).g = _mm_and_si128((a).g, (b).g)
#define vor(dst, a, b) \
	(dst).f = _mm256_or_ps((a).f, (b).f); \
	(dst).g = _mm_or_si128((a).g, (b).g)
#define vandn(dst, a, b) \
	(dst).f = _mm256_andnot_ps((b).f, (a).f); \
	(dst).g = _mm_andnot_si128((b).g, (a).g)
#define vxorn(dst, a, b) \
	(dst).f = _mm256_xor_ps(_mm256_xor_ps((a).f, (b).f), \
	    (*(vtype *)&DES_bs_all.ones).f); \
	(dst).g = _mm_xor_si128(_mm_xor_si128((a).g, (b).g), \
	    (*(vtype *)&DES_bs_all.ones).g)

#ifdef __XOP__
#define vnor(dst, a, b) \
	(dst).f = _mm256_xor_ps(_mm256_or_ps((a).f, (b).f), \
	    (*(vtype *)&DES_bs_all.ones).f); \
	(dst).g = _mm_xor_si128(_mm_or_si128((a).g, (b).g), \
	    (*(vtype *)&DES_bs_all.ones).g)
/* This could be _mm256_cmov_ps(), but it does not exist (yet?) */
#define vsel(dst, a, b, c) \
	(dst).f = __builtin_ia32_vpcmov_v8sf256((b).f, (a).f, (c).f); \
	(dst).g = _mm_cmov_si128((b).g, (a).g, (c).g)
#endif

#elif defined(__AVX__) && DES_BS_DEPTH == 512
#undef DES_BS_VECTOR

#include <immintrin.h>

typedef struct {
/* Not __m256i because bitwise ops are "floating-point" with AVX */
	__m256 f, g;
} vtype;

#define vst(dst, ofs, src) \
	_mm256_store_ps( \
	    (float *)&((vtype *)((DES_bs_vector *)&(dst) + (ofs)))->f, \
	    (src).f); \
	_mm256_store_ps( \
	    (float *)&((vtype *)((DES_bs_vector *)&(dst) + (ofs)))->g, \
	    (src).g)

#define vxor(dst, a, b) \
	(dst).f = _mm256_xor_ps((a).f, (b).f); \
	(dst).g = _mm256_xor_ps((a).g, (b).g)

#define vnot(dst, a) \
	(dst).f = _mm256_xor_ps((a).f, ((vtype *)&DES_bs_all.ones)->f); \
	(dst).g = _mm256_xor_ps((a).g, ((vtype *)&DES_bs_all.ones)->g)
#define vand(dst, a, b) \
	(dst).f = _mm256_and_ps((a).f, (b).f); \
	(dst).g = _mm256_and_ps((a).g, (b).g)
#define vor(dst, a, b) \
	(dst).f = _mm256_or_ps((a).f, (b).f); \
	(dst).g = _mm256_or_ps((a).g, (b).g)
#define vandn(dst, a, b) \
	(dst).f = _mm256_andnot_ps((b).f, (a).f); \
	(dst).g = _mm256_andnot_ps((b).g, (a).g)
#define vxorn(dst, a, b) \
	(dst).f = _mm256_xor_ps(_mm256_xor_ps((a).f, (b).f), \
	    (*(vtype *)&DES_bs_all.ones).f); \
	(dst).g = _mm256_xor_ps(_mm256_xor_ps((a).g, (b).g), \
	    (*(vtype *)&DES_bs_all.ones).g)

#ifdef __XOP__
#define vnor(dst, a, b) \
	(dst).f = _mm256_xor_ps(_mm256_or_ps((a).f, (b).f), \
	    (*(vtype *)&DES_bs_all.ones).f); \
	(dst).g = _mm256_xor_ps(_mm256_or_ps((a).g, (b).g), \
	    (*(vtype *)&DES_bs_all.ones).g)
/* This could be _mm256_cmov_ps(), but it does not exist (yet?) */
#define vsel(dst, a, b, c) \
	(dst).f = __builtin_ia32_vpcmov_v8sf256((b).f, (a).f, (c).f); \
	(dst).g = __builtin_ia32_vpcmov_v8sf256((b).g, (a).g, (c).g)
#endif

#elif defined(__AVX__) && defined(__MMX__) && DES_BS_DEPTH == 320 && \
    !defined(DES_BS_NO_MMX)
#undef DES_BS_VECTOR

#include <immintrin.h>
#include <mmintrin.h>

typedef struct {
/* Not __m256i because bitwise ops are "floating-point" with AVX */
	__m256 f;
	__m64 g;
} vtype;

#define vst(dst, ofs, src) \
	_mm256_store_ps( \
	    (float *)&((vtype *)((DES_bs_vector *)&(dst) + (ofs)))->f, \
	    (src).f); \
	((vtype *)((DES_bs_vector *)&(dst) + (ofs)))->g = (src).g

#define vxor(dst, a, b) \
	(dst).f = _mm256_xor_ps((a).f, (b).f); \
	(dst).g = _mm_xor_si64((a).g, (b).g)

#define vnot(dst, a) \
	(dst).f = _mm256_xor_ps((a).f, ((vtype *)&DES_bs_all.ones)->f); \
	(dst).g = _mm_xor_si64((a).g, ((vtype *)&DES_bs_all.ones)->g)
#define vand(dst, a, b) \
	(dst).f = _mm256_and_ps((a).f, (b).f); \
	(dst).g = _mm_and_si64((a).g, (b).g)
#define vor(dst, a, b) \
	(dst).f = _mm256_or_ps((a).f, (b).f); \
	(dst).g = _mm_or_si64((a).g, (b).g)
#define vandn(dst, a, b) \
	(dst).f = _mm256_andnot_ps((b).f, (a).f); \
	(dst).g = _mm_andnot_si64((b).g, (a).g)
#define vxorn(dst, a, b) \
	(dst).f = _mm256_xor_ps(_mm256_xor_ps((a).f, (b).f), \
	    (*(vtype *)&DES_bs_all.ones).f); \
	(dst).g = _mm_xor_si64(_mm_xor_si64((a).g, (b).g), \
	    (*(vtype *)&DES_bs_all.ones).g)

#elif defined(__AVX__) && \
    ((ARCH_BITS == 64 && DES_BS_DEPTH == 320) || \
    (ARCH_BITS == 32 && DES_BS_DEPTH == 288))
#undef DES_BS_VECTOR

#include <immintrin.h>
#include <mmintrin.h>

typedef struct {
/* Not __m256i because bitwise ops are "floating-point" with AVX */
	__m256 f;
	ARCH_WORD g;
} vtype;

#define vst(dst, ofs, src) \
	_mm256_store_ps( \
	    (float *)&((vtype *)((DES_bs_vector *)&(dst) + (ofs)))->f, \
	    (src).f); \
	((vtype *)((DES_bs_vector *)&(dst) + (ofs)))->g = (src).g

#define vxor(dst, a, b) \
	(dst).f = _mm256_xor_ps((a).f, (b).f); \
	(dst).g = (a).g ^ (b).g

#define vnot(dst, a) \
	(dst).f = _mm256_xor_ps((a).f, ((vtype *)&DES_bs_all.ones)->f); \
	(dst).g = ~(a).g
#define vand(dst, a, b) \
	(dst).f = _mm256_and_ps((a).f, (b).f); \
	(dst).g = (a).g & (b).g
#define vor(dst, a, b) \
	(dst).f = _mm256_or_ps((a).f, (b).f); \
	(dst).g = (a).g | (b).g
#define vandn(dst, a, b) \
	(dst).f = _mm256_andnot_ps((b).f, (a).f); \
	(dst).g = (a).g & ~(b).g
#define vxorn(dst, a, b) \
	(dst).f = _mm256_xor_ps(_mm256_xor_ps((a).f, (b).f), \
	    (*(vtype *)&DES_bs_all.ones).f); \
	(dst).g = ~((a).g ^ (b).g)

#elif defined(__AVX__) && defined(__MMX__) && \
    ((ARCH_BITS == 64 && DES_BS_DEPTH == 384) || \
    (ARCH_BITS == 32 && DES_BS_DEPTH == 352))
#undef DES_BS_VECTOR

#include <immintrin.h>
#include <mmintrin.h>

typedef struct {
/* Not __m256i because bitwise ops are "floating-point" with AVX */
	__m256 f;
	__m64 g;
	ARCH_WORD h;
} vtype;

#define vst(dst, ofs, src) \
	_mm256_store_ps( \
	    (float *)&((vtype *)((DES_bs_vector *)&(dst) + (ofs)))->f, \
	    (src).f); \
	((vtype *)((DES_bs_vector *)&(dst) + (ofs)))->g = (src).g; \
	((vtype *)((DES_bs_vector *)&(dst) + (ofs)))->h = (src).h

#define vxor(dst, a, b) \
	(dst).f = _mm256_xor_ps((a).f, (b).f); \
	(dst).g = _mm_xor_si64((a).g, (b).g); \
	(dst).h = (a).h ^ (b).h

#define vnot(dst, a) \
	(dst).f = _mm256_xor_ps((a).f, ((vtype *)&DES_bs_all.ones)->f); \
	(dst).g = _mm_xor_si64((a).g, ((vtype *)&DES_bs_all.ones)->g); \
	(dst).h = ~(a).h
#define vand(dst, a, b) \
	(dst).f = _mm256_and_ps((a).f, (b).f); \
	(dst).g = _mm_and_si64((a).g, (b).g); \
	(dst).h = (a).h & (b).h
#define vor(dst, a, b) \
	(dst).f = _mm256_or_ps((a).f, (b).f); \
	(dst).g = _mm_or_si64((a).g, (b).g); \
	(dst).h = (a).h | (b).h
#define vandn(dst, a, b) \
	(dst).f = _mm256_andnot_ps((b).f, (a).f); \
	(dst).g = _mm_andnot_si64((b).g, (a).g); \
	(dst).h = (a).h & ~(b).h
#define vxorn(dst, a, b) \
	(dst).f = _mm256_xor_ps(_mm256_xor_ps((a).f, (b).f), \
	    (*(vtype *)&DES_bs_all.ones).f); \
	(dst).g = _mm_xor_si64(_mm_xor_si64((a).g, (b).g), \
	    (*(vtype *)&DES_bs_all.ones).g); \
	(dst).h = ~((a).h ^ (b).h)

#elif defined(__SSE2__) && DES_BS_DEPTH == 128
#undef DES_BS_VECTOR

#if defined(__GNUC__) && !defined(__AVX__)
#warning Notice: with recent versions of gcc, we are currently using SSE2 intrinsics instead of the supplied SSE2 assembly code.  This choice is made in the x86-*.h file.
#endif

#ifdef __AVX__
#include <immintrin.h>
#ifdef __XOP__
#include <x86intrin.h>
#endif
#else
#include <emmintrin.h>
#endif

typedef __m128i vtype;

#define vst(dst, ofs, src) \
	_mm_store_si128((vtype *)((DES_bs_vector *)&(dst) + (ofs)), (src))

#define vxorf(a, b) \
	_mm_xor_si128((a), (b))

#define vnot(dst, a) \
	(dst) = _mm_xor_si128((a), *(vtype *)&DES_bs_all.ones)
#define vand(dst, a, b) \
	(dst) = _mm_and_si128((a), (b))
#define vor(dst, a, b) \
	(dst) = _mm_or_si128((a), (b))
#define vandn(dst, a, b) \
	(dst) = _mm_andnot_si128((b), (a))
#define vxorn(dst, a, b) \
	(dst) = _mm_xor_si128(_mm_xor_si128((a), (b)), \
	    *(vtype *)&DES_bs_all.ones)
#define vnor(dst, a, b) \
	(dst) = _mm_xor_si128(_mm_or_si128((a), (b)), \
	    *(vtype *)&DES_bs_all.ones)

#ifdef __XOP__
#define vsel(dst, a, b, c) \
	(dst) = _mm_cmov_si128((b), (a), (c))
#else
#define vsel(dst, a, b, c) \
	(dst) = _mm_xor_si128(_mm_andnot_si128((c), (a)), \
	    _mm_and_si128((c), (b)))
#endif

#elif defined(__SSE2__) && DES_BS_DEPTH == 256
#undef DES_BS_VECTOR

#ifdef __AVX__
#include <immintrin.h>
#ifdef __XOP__
#include <x86intrin.h>
#endif
#else
#include <emmintrin.h>
#endif

typedef struct {
	__m128i f, g;
} vtype;

#define vst(dst, ofs, src) \
	_mm_store_si128(&((vtype *)((DES_bs_vector *)&(dst) + (ofs)))->f, \
	    (src).f); \
	_mm_store_si128(&((vtype *)((DES_bs_vector *)&(dst) + (ofs)))->g, \
	    (src).g)

#define vxor(dst, a, b) \
	(dst).f = _mm_xor_si128((a).f, (b).f); \
	(dst).g = _mm_xor_si128((a).g, (b).g)

#define vnot(dst, a) \
	(dst).f = _mm_xor_si128((a).f, ((vtype *)&DES_bs_all.ones)->f); \
	(dst).g = _mm_xor_si128((a).g, ((vtype *)&DES_bs_all.ones)->g)
#define vand(dst, a, b) \
	(dst).f = _mm_and_si128((a).f, (b).f); \
	(dst).g = _mm_and_si128((a).g, (b).g)
#define vor(dst, a, b) \
	(dst).f = _mm_or_si128((a).f, (b).f); \
	(dst).g = _mm_or_si128((a).g, (b).g)
#define vandn(dst, a, b) \
	(dst).f = _mm_andnot_si128((b).f, (a).f); \
	(dst).g = _mm_andnot_si128((b).g, (a).g)
#define vxorn(dst, a, b) \
	(dst).f = _mm_xor_si128(_mm_xor_si128((a).f, (b).f), \
	    (*(vtype *)&DES_bs_all.ones).f); \
	(dst).g = _mm_xor_si128(_mm_xor_si128((a).g, (b).g), \
	    (*(vtype *)&DES_bs_all.ones).g)

#ifdef __XOP__
#define vnor(dst, a, b) \
	(dst).f = _mm_xor_si128(_mm_or_si128((a).f, (b).f), \
	    (*(vtype *)&DES_bs_all.ones).f); \
	(dst).g = _mm_xor_si128(_mm_or_si128((a).g, (b).g), \
	    (*(vtype *)&DES_bs_all.ones).g)
#define vsel(dst, a, b, c) \
	(dst).f = _mm_cmov_si128((b).f, (a).f, (c).f); \
	(dst).g = _mm_cmov_si128((b).g, (a).g, (c).g)
#endif

#elif defined(__SSE2__) && defined(__MMX__) && DES_BS_DEPTH == 192 && \
    !defined(DES_BS_NO_MMX)
#undef DES_BS_VECTOR

#include <emmintrin.h>
#include <mmintrin.h>

typedef struct {
	__m128i f;
	__m64 g;
} vtype;

#define vst(dst, ofs, src) \
	_mm_store_si128(&((vtype *)((DES_bs_vector *)&(dst) + (ofs)))->f, \
	    (src).f); \
	((vtype *)((DES_bs_vector *)&(dst) + (ofs)))->g = (src).g

#define vxor(dst, a, b) \
	(dst).f = _mm_xor_si128((a).f, (b).f); \
	(dst).g = _mm_xor_si64((a).g, (b).g)

#define vnot(dst, a) \
	(dst).f = _mm_xor_si128((a).f, ((vtype *)&DES_bs_all.ones)->f); \
	(dst).g = _mm_xor_si64((a).g, ((vtype *)&DES_bs_all.ones)->g)
#define vand(dst, a, b) \
	(dst).f = _mm_and_si128((a).f, (b).f); \
	(dst).g = _mm_and_si64((a).g, (b).g)
#define vor(dst, a, b) \
	(dst).f = _mm_or_si128((a).f, (b).f); \
	(dst).g = _mm_or_si64((a).g, (b).g)
#define vandn(dst, a, b) \
	(dst).f = _mm_andnot_si128((b).f, (a).f); \
	(dst).g = _mm_andnot_si64((b).g, (a).g)
#define vxorn(dst, a, b) \
	(dst).f = _mm_xor_si128(_mm_xor_si128((a).f, (b).f), \
	    (*(vtype *)&DES_bs_all.ones).f); \
	(dst).g = _mm_xor_si64(_mm_xor_si64((a).g, (b).g), \
	    (*(vtype *)&DES_bs_all.ones).g)

#elif defined(__SSE2__) && \
    ((ARCH_BITS == 64 && DES_BS_DEPTH == 192) || \
    (ARCH_BITS == 32 && DES_BS_DEPTH == 160))
#undef DES_BS_VECTOR

#include <emmintrin.h>

typedef struct {
	__m128i f;
	ARCH_WORD g;
} vtype;

#define vst(dst, ofs, src) \
	_mm_store_si128(&((vtype *)((DES_bs_vector *)&(dst) + (ofs)))->f, \
	    (src).f); \
	((vtype *)((DES_bs_vector *)&(dst) + (ofs)))->g = (src).g

#define vxor(dst, a, b) \
	(dst).f = _mm_xor_si128((a).f, (b).f); \
	(dst).g = (a).g ^ (b).g

#define vnot(dst, a) \
	(dst).f = _mm_xor_si128((a).f, ((vtype *)&DES_bs_all.ones)->f); \
	(dst).g = ~(a).g
#define vand(dst, a, b) \
	(dst).f = _mm_and_si128((a).f, (b).f); \
	(dst).g = (a).g & (b).g
#define vor(dst, a, b) \
	(dst).f = _mm_or_si128((a).f, (b).f); \
	(dst).g = (a).g | (b).g
#define vandn(dst, a, b) \
	(dst).f = _mm_andnot_si128((b).f, (a).f); \
	(dst).g = (a).g & ~(b).g
#define vxorn(dst, a, b) \
	(dst).f = _mm_xor_si128(_mm_xor_si128((a).f, (b).f), \
	    (*(vtype *)&DES_bs_all.ones).f); \
	(dst).g = ~((a).g ^ (b).g)

#elif defined(__SSE2__) && defined(__MMX__) && \
    ((ARCH_BITS == 64 && DES_BS_DEPTH == 256) || \
    (ARCH_BITS == 32 && DES_BS_DEPTH == 224))
#undef DES_BS_VECTOR

#include <emmintrin.h>
#include <mmintrin.h>

typedef struct {
	__m128i f;
	__m64 g;
	ARCH_WORD h;
} vtype;

#define vst(dst, ofs, src) \
	_mm_store_si128(&((vtype *)((DES_bs_vector *)&(dst) + (ofs)))->f, \
	    (src).f); \
	((vtype *)((DES_bs_vector *)&(dst) + (ofs)))->g = (src).g; \
	((vtype *)((DES_bs_vector *)&(dst) + (ofs)))->h = (src).h

#define vxor(dst, a, b) \
	(dst).f = _mm_xor_si128((a).f, (b).f); \
	(dst).g = _mm_xor_si64((a).g, (b).g); \
	(dst).h = (a).h ^ (b).h

#define vnot(dst, a) \
	(dst).f = _mm_xor_si128((a).f, ((vtype *)&DES_bs_all.ones)->f); \
	(dst).g = _mm_xor_si64((a).g, ((vtype *)&DES_bs_all.ones)->g); \
	(dst).h = ~(a).h
#define vand(dst, a, b) \
	(dst).f = _mm_and_si128((a).f, (b).f); \
	(dst).g = _mm_and_si64((a).g, (b).g); \
	(dst).h = (a).h & (b).h
#define vor(dst, a, b) \
	(dst).f = _mm_or_si128((a).f, (b).f); \
	(dst).g = _mm_or_si64((a).g, (b).g); \
	(dst).h = (a).h | (b).h
#define vandn(dst, a, b) \
	(dst).f = _mm_andnot_si128((b).f, (a).f); \
	(dst).g = _mm_andnot_si64((b).g, (a).g); \
	(dst).h = (a).h & ~(b).h
#define vxorn(dst, a, b) \
	(dst).f = _mm_xor_si128(_mm_xor_si128((a).f, (b).f), \
	    (*(vtype *)&DES_bs_all.ones).f); \
	(dst).g = _mm_xor_si64(_mm_xor_si64((a).g, (b).g), \
	    (*(vtype *)&DES_bs_all.ones).g); \
	(dst).h = ~((a).h ^ (b).h)

#elif defined(__MMX__) && ARCH_BITS != 64 && DES_BS_DEPTH == 64
#undef DES_BS_VECTOR

#include <mmintrin.h>

typedef __m64 vtype;

#define vxorf(a, b) \
	_mm_xor_si64((a), (b))

#define vnot(dst, a) \
	(dst) = _mm_xor_si64((a), *(vtype *)&DES_bs_all.ones)
#define vand(dst, a, b) \
	(dst) = _mm_and_si64((a), (b))
#define vor(dst, a, b) \
	(dst) = _mm_or_si64((a), (b))
#define vandn(dst, a, b) \
	(dst) = _mm_andnot_si64((b), (a))
#define vxorn(dst, a, b) \
	(dst) = _mm_xor_si64(_mm_xor_si64((a), (b)), \
	    *(vtype *)&DES_bs_all.ones)

#elif defined(__MMX__) && ARCH_BITS == 32 && DES_BS_DEPTH == 96
#undef DES_BS_VECTOR

#include <mmintrin.h>

typedef struct {
	__m64 f;
	ARCH_WORD g;
} vtype;

#define vst(dst, ofs, src) \
	((vtype *)((DES_bs_vector *)&(dst) + (ofs)))->f = (src).f; \
	((vtype *)((DES_bs_vector *)&(dst) + (ofs)))->g = (src).g

#define vxor(dst, a, b) \
	(dst).f = _mm_xor_si64((a).f, (b).f); \
	(dst).g = (a).g ^ (b).g

#define vnot(dst, a) \
	(dst).f = _mm_xor_si64((a).f, ((vtype *)&DES_bs_all.ones)->f); \
	(dst).g = ~(a).g
#define vand(dst, a, b) \
	(dst).f = _mm_and_si64((a).f, (b).f); \
	(dst).g = (a).g & (b).g
#define vor(dst, a, b) \
	(dst).f = _mm_or_si64((a).f, (b).f); \
	(dst).g = (a).g | (b).g
#define vandn(dst, a, b) \
	(dst).f = _mm_andnot_si64((b).f, (a).f); \
	(dst).g = (a).g & ~(b).g
#define vxorn(dst, a, b) \
	(dst).f = _mm_xor_si64(_mm_xor_si64((a).f, (b).f), \
	    (*(vtype *)&DES_bs_all.ones).f); \
	(dst).g = ~((a).g ^ (b).g)

#else

typedef ARCH_WORD vtype;

#define zero				0
#define ones				~(vtype)0

#define vxorf(a, b) \
	((a) ^ (b))

#define vnot(dst, a) \
	(dst) = ~(a)
#define vand(dst, a, b) \
	(dst) = (a) & (b)
#define vor(dst, a, b) \
	(dst) = (a) | (b)
#define vandn(dst, a, b) \
	(dst) = (a) & ~(b)
#define vxorn(dst, a, b) \
	(dst) = ~((a) ^ (b))
#define vnor(dst, a, b) \
	(dst) = ~((a) | (b))
#define vsel(dst, a, b, c) \
	(dst) = (((a) & ~(c)) ^ ((b) & (c)))

#endif

#ifndef vst
#define vst(dst, ofs, src) \
	*((vtype *)((DES_bs_vector *)&(dst) + (ofs))) = (src)
#endif

#if !defined(vxor) && defined(vxorf)
#define vxor(dst, a, b) \
	(dst) = vxorf((a), (b))
#endif
#if !defined(vxorf) && defined(vxor)
/*
 * This requires gcc's "Statement Exprs" extension (also supported by a number
 * of other C compilers).
 */
#define vxorf(a, b) \
	({ vtype tmp; vxor(tmp, (a), (b)); tmp; })
#endif

#ifdef __GNUC__
#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
#define MAYBE_INLINE __attribute__((always_inline))
#else
#define MAYBE_INLINE __inline__
#endif
#else
#define MAYBE_INLINE
#endif

/* Include the S-boxes here so that the compiler can inline them */
#if DES_BS == 3
#include "sboxes-s.c"
#elif DES_BS == 2
#include "sboxes.c"
#else
#undef andn
#include "nonstd.c"
#endif

#define b				DES_bs_all.B
#define e				DES_bs_all.E.E

#ifndef DES_BS_VECTOR
#define DES_BS_VECTOR			0
#endif

#if DES_BS_VECTOR
#define kd				[depth]
#define bd				[depth]
#define ed				[depth]
#define for_each_depth() \
	for (depth = 0; depth < DES_BS_VECTOR; depth++)
#else
#if DES_BS_EXPAND
#define kd
#else
#define kd				[0]
#endif
#define bd
#define ed				[0]
#define for_each_depth()
#endif

#define DES_bs_clear_block_8(i) \
	for_each_depth() { \
		vst(b[i] bd, 0, zero); \
		vst(b[i] bd, 1, zero); \
		vst(b[i] bd, 2, zero); \
		vst(b[i] bd, 3, zero); \
		vst(b[i] bd, 4, zero); \
		vst(b[i] bd, 5, zero); \
		vst(b[i] bd, 6, zero); \
		vst(b[i] bd, 7, zero); \
	}

#define DES_bs_clear_block() \
	DES_bs_clear_block_8(0); \
	DES_bs_clear_block_8(8); \
	DES_bs_clear_block_8(16); \
	DES_bs_clear_block_8(24); \
	DES_bs_clear_block_8(32); \
	DES_bs_clear_block_8(40); \
	DES_bs_clear_block_8(48); \
	DES_bs_clear_block_8(56);

#define DES_bs_set_block_8(i, v0, v1, v2, v3, v4, v5, v6, v7) \
	for_each_depth() { \
		vst(b[i] bd, 0, v0); \
		vst(b[i] bd, 1, v1); \
		vst(b[i] bd, 2, v2); \
		vst(b[i] bd, 3, v3); \
		vst(b[i] bd, 4, v4); \
		vst(b[i] bd, 5, v5); \
		vst(b[i] bd, 6, v6); \
		vst(b[i] bd, 7, v7); \
	}

#define x(p) vxorf(*(vtype *)&e[p] ed, *(vtype *)&k[p] kd)
#define y(p, q) vxorf(*(vtype *)&b[p] bd, *(vtype *)&k[q] kd)
#define z(r) ((vtype *)&b[r] bd)

void DES_bs_crypt(int count)
{
#if DES_BS_EXPAND
	DES_bs_vector *k;
#else
	ARCH_WORD **k;
#endif
	int iterations, rounds_and_swapped;
#if DES_BS_VECTOR
	int depth;
#endif

#ifndef zero
	vtype zero;
/* This may produce an "uninitialized" warning */
	vxor(zero, zero, zero);
#endif

	DES_bs_clear_block();

#if DES_BS_EXPAND
	k = DES_bs_all.KS.v;
#else
	k = DES_bs_all.KS.p;
#endif
	rounds_and_swapped = 8;
	iterations = count;

start:
	for_each_depth()
	s1(x(0), x(1), x(2), x(3), x(4), x(5),
		z(40), z(48), z(54), z(62));
	for_each_depth()
	s2(x(6), x(7), x(8), x(9), x(10), x(11),
		z(44), z(59), z(33), z(49));
	for_each_depth()
	s3(x(12), x(13), x(14), x(15), x(16), x(17),
		z(55), z(47), z(61), z(37));
	for_each_depth()
	s4(x(18), x(19), x(20), x(21), x(22), x(23),
		z(57), z(51), z(41), z(32));
	for_each_depth()
	s5(x(24), x(25), x(26), x(27), x(28), x(29),
		z(39), z(45), z(56), z(34));
	for_each_depth()
	s6(x(30), x(31), x(32), x(33), x(34), x(35),
		z(35), z(60), z(42), z(50));
	for_each_depth()
	s7(x(36), x(37), x(38), x(39), x(40), x(41),
		z(63), z(43), z(53), z(38));
	for_each_depth()
	s8(x(42), x(43), x(44), x(45), x(46), x(47),
		z(36), z(58), z(46), z(52));

	if (rounds_and_swapped == 0x100) goto next;

swap:
	for_each_depth()
	s1(x(48), x(49), x(50), x(51), x(52), x(53),
		z(8), z(16), z(22), z(30));
	for_each_depth()
	s2(x(54), x(55), x(56), x(57), x(58), x(59),
		z(12), z(27), z(1), z(17));
	for_each_depth()
	s3(x(60), x(61), x(62), x(63), x(64), x(65),
		z(23), z(15), z(29), z(5));
	for_each_depth()
	s4(x(66), x(67), x(68), x(69), x(70), x(71),
		z(25), z(19), z(9), z(0));
	for_each_depth()
	s5(x(72), x(73), x(74), x(75), x(76), x(77),
		z(7), z(13), z(24), z(2));
	for_each_depth()
	s6(x(78), x(79), x(80), x(81), x(82), x(83),
		z(3), z(28), z(10), z(18));
	for_each_depth()
	s7(x(84), x(85), x(86), x(87), x(88), x(89),
		z(31), z(11), z(21), z(6));
	for_each_depth()
	s8(x(90), x(91), x(92), x(93), x(94), x(95),
		z(4), z(26), z(14), z(20));

	k += 96;

	if (--rounds_and_swapped) goto start;
	k -= (0x300 + 48);
	rounds_and_swapped = 0x108;
	if (--iterations) goto swap;
	return;

next:
	k -= (0x300 - 48);
	rounds_and_swapped = 8;
	if (--iterations) goto start;
}

void DES_bs_crypt_25(void)
{
#if DES_BS_EXPAND
	DES_bs_vector *k;
#else
	ARCH_WORD **k;
#endif
	int iterations, rounds_and_swapped;
#if DES_BS_VECTOR
	int depth;
#endif

#ifndef zero
	vtype zero;
/* This may produce an "uninitialized" warning */
	vxor(zero, zero, zero);
#endif

	DES_bs_clear_block();

#if DES_BS_EXPAND
	k = DES_bs_all.KS.v;
#else
	k = DES_bs_all.KS.p;
#endif
	rounds_and_swapped = 8;
	iterations = 25;

start:
	for_each_depth()
	s1(x(0), x(1), x(2), x(3), x(4), x(5),
		z(40), z(48), z(54), z(62));
	for_each_depth()
	s2(x(6), x(7), x(8), x(9), x(10), x(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(x(24), x(25), x(26), x(27), x(28), x(29),
		z(39), z(45), z(56), z(34));
	for_each_depth()
	s6(x(30), x(31), x(32), x(33), x(34), x(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));

	if (rounds_and_swapped == 0x100) goto next;

swap:
	for_each_depth()
	s1(x(48), x(49), x(50), x(51), x(52), x(53),
		z(8), z(16), z(22), z(30));
	for_each_depth()
	s2(x(54), x(55), x(56), x(57), x(58), x(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(x(72), x(73), x(74), x(75), x(76), x(77),
		z(7), z(13), z(24), z(2));
	for_each_depth()
	s6(x(78), x(79), x(80), x(81), x(82), x(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;

	if (--rounds_and_swapped) goto start;
	k -= (0x300 + 48);
	rounds_and_swapped = 0x108;
	if (--iterations) goto swap;
	return;

next:
	k -= (0x300 - 48);
	rounds_and_swapped = 8;
	iterations--;
	goto start;
}

#undef x

#undef kd
#if DES_BS_VECTOR
#define kd				[depth]
#else
#define kd				[0]
#endif

void DES_bs_crypt_LM(void)
{
	ARCH_WORD **k;
	int rounds;
#if DES_BS_VECTOR
	int depth;
#endif

#ifndef zero
	vtype zero, ones;
/* This may produce an "uninitialized" warning */
	vxor(zero, zero, zero);
	vnot(ones, zero);
#endif
/*
//
0100101101000111010100110010000101000000001000110010010000100101

0000000011101000000000010010011100010111000001001100001010101111


//11110101 01000011 00100000 11101000 11100100 10000000 00010111 00000000


//0100101101000111010100110010000101000000001000110010010000100101


static 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,
	63, 55, 47, 39, 31, 23, 15, 7,
	56, 48, 40, 32, 24, 16, 8, 0,
	58, 50, 42, 34, 26, 18, 10, 2,
	60, 52, 44, 36, 28, 20, 12, 4,
	62, 54, 46, 38, 30, 22, 14, 6
};


*/
	DES_bs_set_block_8(0, zero, zero, zero, zero, zero, zero, zero, zero);
	DES_bs_set_block_8(8, ones, ones, ones, zero, ones, zero, zero, zero);
	DES_bs_set_block_8(16, zero, zero, zero, zero, zero, zero, zero, ones);
	DES_bs_set_block_8(24, zero, zero, ones, zero, zero, ones, ones, ones);
	DES_bs_set_block_8(32, zero, zero, zero, ones, zero, ones, ones, ones);
	DES_bs_set_block_8(40, zero, zero, zero, zero, zero, ones, zero, zero);
	DES_bs_set_block_8(48, ones, ones, zero, zero, zero, zero, ones, zero);
	DES_bs_set_block_8(56, ones, zero, ones, zero, ones, ones, ones, ones);




	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);
}
#define b				DES_bs_all.B


void DES_bs_crypt_Oracle(ARCH_WORD *binary_plain_salt_mix){
	




ARCH_WORD **k;
	int rounds,ofs,bit,dst;


//printf("Ciphertext");
//for(bit=0;bit<64;bit++)
// printf("%u",binary_plain_salt_mix[bit]);



/*{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
  0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 
  0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0}
*/

/*{1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0 <repeats 11 times>, 1, 0, 
  1, 0, 1, 0 <repeats 17 times>, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 
  0}
*/

        ARCH_WORD *bnew=NULL;
         ARCH_WORD temp_binary_plain_salt_mix[64];

for(bit=0;bit<64;bit++)
 temp_binary_plain_salt_mix[bit]=0;


static 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,
	63, 55, 47, 39, 31, 23, 15, 7,
	56, 48, 40, 32, 24, 16, 8, 0,
	58, 50, 42, 34, 26, 18, 10, 2,
	60, 52, 44, 36, 28, 20, 12, 4,
	62, 54, 46, 38, 30, 22, 14, 6
};


   
#if DES_BS_VECTOR
	int depth;
#endif


#ifndef zero
	vtype zero, ones;
/* This may produce an "uninitialized" warning */
	vxor(zero, zero, zero);
	vnot(ones, zero);
#endif

for (dst = 0; dst < 64; dst++) {
		int src = DES_IP[dst ^ 0x20];
	temp_binary_plain_salt_mix[dst]=binary_plain_salt_mix[src];
}
DES_bs_clear_block();


for_each_depth(){
for (bit = 0; bit<64; bit++)

  if(temp_binary_plain_salt_mix[bit]==0)
   vst(b[0] bd, bit,zero); 
  else
   vst(b[0] bd, bit,ones); 
   

}

/*printf("Bnew value..depth1\n");
for(bit=0;bit<64;bit++)
  printf("%u",(b[bit][0]&1));
printf("\n");

printf("Bnew value..depth2\n");
for(bit=0;bit<64;bit++)
  printf("%u",(b[bit][1]&1));
printf("\n");
 */
  





	k = DES_bs_all.KS.p;


printf("Key values..\n");
for(bit=0;bit<768;bit++)
  printf("%u",(k[bit][0]));
printf("\n");



	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);




}



#endif

/*
 * This file is part of John the Ripper password cracker,
 * Copyright (c) 1996-2002,2005,2010 by Solar Designer
 */

#include <string.h>

#include "arch.h"
#include "common.h"
#include "DES_std.h"
#include "DES_bs.h"
#include "unicode.h"

#if DES_BS_VECTOR
#define DEPTH				[depth]
#define START				[0]
#define init_depth() \
	int depth; \
	depth = index >> ARCH_BITS_LOG; \
	index &= (ARCH_BITS - 1);
#define for_each_depth() \
	for (depth = 0; depth < DES_BS_VECTOR; depth++)
#else
#define DEPTH
#define START
#define init_depth()
#define for_each_depth()
#endif

#if !DES_BS_ASM
DES_bs_combined CC_CACHE_ALIGN DES_bs_all;
#endif



static 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,
	63, 55, 47, 39, 31, 23, 15, 7,
	56, 48, 40, 32, 24, 16, 8, 0,
	58, 50, 42, 34, 26, 18, 10, 2,
	60, 52, 44, 36, 28, 20, 12, 4,
	62, 54, 46, 38, 30, 22, 14, 6
};

//0000000101110000010001010011111010001001111110001100110110100111
//11111100 01000001001010011000001000000101001101101001101011011111


unsigned char t[8];//new modification
static unsigned char DES_LM_KP[56] = {
	1, 2, 3, 4, 5, 6, 7,
	10, 11, 12, 13, 14, 15, 0,
	19, 20, 21, 22, 23, 8, 9,
	28, 29, 30, 31, 16, 17, 18,
	37, 38, 39, 24, 25, 26, 27,
	46, 47, 32, 33, 34, 35, 36,
	55, 40, 41, 42, 43, 44, 45,
	48, 49, 50, 51, 52, 53, 54
};

static unsigned char DES_LM_reverse[16] = {
	0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15
};

#if DES_BS_ASM
extern void DES_bs_init_asm(void);
#endif

void DES_bs_init(int flag)
{
	ARCH_WORD **k;
	int round, index, bit;
	int p, q, s;
	int c;

	DES_bs_all.KS_updates = 0;
	if (flag)
		DES_bs_clear_keys_LM();
	else
		DES_bs_clear_keys();

#if DES_BS_EXPAND
	if (flag)
		k = DES_bs_all.KS.p;
	else
		k = DES_bs_all.KSp;
#else
	k = DES_bs_all.KS.p;
#endif

	s = 0;
	for (round = 0; round < 16; round++) {
		s += DES_ROT[round];
		for (index = 0; index < 48; index++) {
			p = DES_PC2[index];
			q = p < 28 ? 0 : 28;
			p += s;
			while (p >= 28) p -= 28;
			bit = DES_PC1[p + q];
			bit ^= 070;
			bit -= bit >> 3;
			bit = 55 - bit;
			if (flag&1) bit = DES_LM_KP[bit];
			*k++ = &DES_bs_all.K[bit] START;
		}
	}

/* Initialize the array with right shifts needed to get past the first
 * non-zero bit in the index. */
	for (bit = 0; bit <= 7; bit++)
	for (index = 1 << bit; index < 0x100; index += 2 << bit)
		DES_bs_all.s1[index] = bit + 1;

/* Special case: instead of doing an extra check in *_set_key*(), we
 * might overrun into DES_bs_all.B, which is harmless as long as the
 * order of fields is unchanged.  57 is the smallest value to guarantee
 * we'd be past the end of K[] since we start at -1. */
	DES_bs_all.s1[0] = 57;

/* The same for second bits */
	for (index = 0; index < 0x100; index++) {
		bit = DES_bs_all.s1[index];
		bit += DES_bs_all.s1[index >> bit];
		DES_bs_all.s2[index] = (bit <= 8) ? bit : 57;
	}

/* Convert to byte offsets */
	for (index = 0; index < 0x100; index++)
		DES_bs_all.s1[index] *= sizeof(DES_bs_vector);

	if (flag) {
		for (c = 0; c < 0x100; c++)
#ifdef BENCH_BUILD
		if (c >= 'a' && c <= 'z')
			DES_bs_all.E.extras.u[c] = c & ~0x20;
		else
			DES_bs_all.E.extras.u[c] = c;
#else
			DES_bs_all.E.extras.u[c] = CP_up[c];
#endif
	}

#if DES_BS_ASM
	DES_bs_init_asm();
#elif defined(__MMX__) || defined(__SSE2__)
	memset(&DES_bs_all.ones, -1, sizeof(DES_bs_all.ones));
#endif
}

void DES_bs_set_salt(ARCH_WORD salt)
{
	ARCH_WORD mask;
	int src, dst;

	mask = 1;
	for (dst = 0; dst < 48; dst++) {
		if (dst == 24) mask = 1;

		if (salt & mask) {
			if (dst < 24) src = dst + 24; else src = dst - 24;
		} else src = dst;

		DES_bs_all.E.E[dst] = &DES_bs_all.B[DES_E[src]] START;
		DES_bs_all.E.E[dst + 48] = &DES_bs_all.B[DES_E[src] + 32] START;

		mask <<= 1;
	}
}

void DES_bs_clear_keys(void)
{
	if (DES_bs_all.KS_updates++ & 0xFFF) return;
	DES_bs_all.KS_updates = 1;
	memset(DES_bs_all.K, 0, sizeof(DES_bs_all.K));
	memset(DES_bs_all.keys, 0, sizeof(DES_bs_all.keys));
	DES_bs_all.keys_changed = 1;
}

void DES_bs_clear_keys_LM(void)
{
	if (DES_bs_all.KS_updates++ & 0xFFF) return;
	DES_bs_all.KS_updates = 1;
	memset(DES_bs_all.K, 0, sizeof(DES_bs_all.K));
#if !DES_BS_VECTOR && ARCH_BITS >= 64
	memset(DES_bs_all.E.extras.keys, 0, sizeof(DES_bs_all.E.extras.keys));
#else
	memset(DES_bs_all.keys, 0, sizeof(DES_bs_all.keys));
#endif
}

void DES_bs_set_key(char *key, int index)
{
/* new is NUL-terminated, but not NUL-padded to any length;
 * old is NUL-padded to 8 characters, but not always NUL-terminated. */
	unsigned char *new = (unsigned char *)key;
	unsigned char *old = DES_bs_all.keys[index];
	DES_bs_vector *k, *kbase;
	ARCH_WORD mask;
	unsigned int xor, s1, s2;

	init_depth();

	mask = (ARCH_WORD)1 << index;
	k = (DES_bs_vector *)&DES_bs_all.K[0] DEPTH - 1;
#if ARCH_ALLOWS_UNALIGNED
	if (*(ARCH_WORD_32 *)new == *(ARCH_WORD_32 *)old &&
	    old[sizeof(ARCH_WORD_32)]) {
		new += sizeof(ARCH_WORD_32);
		old += sizeof(ARCH_WORD_32);
		k += sizeof(ARCH_WORD_32) * 7;
	}
#endif
	while (*new && k < &DES_bs_all.K[55]) {
		kbase = k;
		if ((xor = *new ^ *old)) {
			xor &= 0x7F; /* Note: this might result in xor == 0 */
			*old = *new;
			do {
				s1 = DES_bs_all.s1[xor];
				s2 = DES_bs_all.s2[xor];
				*(ARCH_WORD *)((char *)k + s1) ^= mask;
				if (s2 > 8) break; /* Required for xor == 0 */
				xor >>= s2;
				k[s2] START ^= mask;
				k += s2;
				if (!xor) break;
				s1 = DES_bs_all.s1[xor];
				s2 = DES_bs_all.s2[xor];
				xor >>= s2;
				*(ARCH_WORD *)((char *)k + s1) ^= mask;
				k[s2] START ^= mask;
				k += s2;
			} while (xor);
		}

		new++;
		old++;
		k = kbase + 7;
	}

	while (*old && k < &DES_bs_all.K[55]) {
		kbase = k;
		xor = *old & 0x7F; /* Note: this might result in xor == 0 */
		*old++ = 0;
		do {
			s1 = DES_bs_all.s1[xor];
			s2 = DES_bs_all.s2[xor];
			*(ARCH_WORD *)((char *)k + s1) ^= mask;
			if (s2 > 8) break; /* Required for xor == 0 */
			xor >>= s2;
			k[s2] START ^= mask;
			k += s2;
			if (!xor) break;
			s1 = DES_bs_all.s1[xor];
			s2 = DES_bs_all.s2[xor];
			xor >>= s2;
			*(ARCH_WORD *)((char *)k + s1) ^= mask;
			k[s2] START ^= mask;
			k += s2;
		} while (xor);

		k = kbase + 7;
	}

	DES_bs_all.keys_changed = 1;
}

void DES_bs_set_key_LM(char *key, int index)
{
/* new is NUL-terminated, but not NUL-padded to any length;
 * old is NUL-padded to 7 characters and NUL-terminated. */
	unsigned char *new = (unsigned char *)key;
#if !DES_BS_VECTOR && ARCH_BITS >= 64
	unsigned char *old = DES_bs_all.E.extras.keys[index];
#else
	unsigned char *old = DES_bs_all.keys[index];
#endif
	DES_bs_vector *k, *kbase;
	ARCH_WORD mask;
	unsigned int xor, s1, s2;
	unsigned char plain;

	init_depth();

	mask = (ARCH_WORD)1 << index;
	k = (DES_bs_vector *)&DES_bs_all.K[0] DEPTH - 1;
#if ARCH_ALLOWS_UNALIGNED
	if (*(ARCH_WORD_32 *)new == *(ARCH_WORD_32 *)old &&
	    old[sizeof(ARCH_WORD_32)]) {
		new += sizeof(ARCH_WORD_32);
		old += sizeof(ARCH_WORD_32);
		k += sizeof(ARCH_WORD_32) * 8;
	}
#endif
	while (*new && k < &DES_bs_all.K[55]) {
		plain = DES_bs_all.E.extras.u[ARCH_INDEX(*new)];
		kbase = k;
		if ((xor = plain ^ *old)) {
			*old = plain;
			do {
				s1 = DES_bs_all.s1[xor];
				s2 = DES_bs_all.s2[xor];
				xor >>= s2;
				*(ARCH_WORD *)((char *)k + s1) ^= mask;
				k[s2] START ^= mask;
				k += s2;
				if (!xor) break;
				s1 = DES_bs_all.s1[xor];
				s2 = DES_bs_all.s2[xor];
				xor >>= s2;
				*(ARCH_WORD *)((char *)k + s1) ^= mask;
				k[s2] START ^= mask;
				k += s2;
			} while (xor);
		}

		new++;
		old++;
		k = kbase + 8;
	}

	while (*old) {
		kbase = k;
		xor = *old;
		*old++ = 0;
		do {
			s1 = DES_bs_all.s1[xor];
			s2 = DES_bs_all.s2[xor];
			xor >>= s2;
			*(ARCH_WORD *)((char *)k + s1) ^= mask;
			k[s2] START ^= mask;
			k += s2;
			if (!xor) break;
			s1 = DES_bs_all.s1[xor];
			s2 = DES_bs_all.s2[xor];
			xor >>= s2;
			*(ARCH_WORD *)((char *)k + s1) ^= mask;
			k[s2] START ^= mask;
			k += s2;
		} while (xor);

		k = kbase + 8;
	}
}

#if DES_BS_EXPAND
void DES_bs_expand_keys(void)
{
	int index;
#if DES_BS_VECTOR
	int depth;
#endif

	if (!DES_bs_all.keys_changed) return;

	for (index = 0; index < 0x300; index++)
	for_each_depth()
#if DES_BS_VECTOR
		DES_bs_all.KS.v[index] DEPTH = DES_bs_all.KSp[index] DEPTH;
#else
		DES_bs_all.KS.v[index] = *DES_bs_all.KSp[index];
#endif

	DES_bs_all.keys_changed = 0;
}
#endif

static ARCH_WORD *DES_bs_get_binary_raw(ARCH_WORD *raw, int count)
{
	static ARCH_WORD out[2];

/* For odd iteration counts, swap L and R here instead of doing it one
 * more time in DES_bs_crypt(). */
	count &= 1;
	out[count] = raw[0];
	out[count ^ 1] = raw[1];

	return out;
}

ARCH_WORD *DES_bs_get_binary(char *ciphertext)
{
	return DES_bs_get_binary_raw(
		DES_raw_get_binary(ciphertext),
		DES_raw_get_count(ciphertext));
}

ARCH_WORD *DES_bs_get_binary_LM(char *ciphertext)
{
	ARCH_WORD block[2], value;
	int l, h;
	int index;

	block[0] = block[1] = 0;
	for (index = 0; index < 16; index += 2) {
		l = atoi16[ARCH_INDEX(ciphertext[index])];
		h = atoi16[ARCH_INDEX(ciphertext[index + 1])];
		value = DES_LM_reverse[l] | (DES_LM_reverse[h] << 4);
		block[index >> 3] |= value << ((index << 2) & 0x18);
	}

	return DES_bs_get_binary_raw(DES_do_IP(block), 1);
}

int DES_bs_get_hash(int index, int count)
{


  //long *piy[2]={{1234567},{456789}};

	int result,bit,dst;
         int temp1[64];
	DES_bs_vector *b;

	init_depth();
	b = (DES_bs_vector *)&DES_bs_all.B[0] DEPTH;




int i;

//unsigned int x[64][2];
//for(i=0;i<64;i++)
  //  x[i][0]=((b[i] START >> index) & 1);
/*
for(i=0;i<64;i++)
    x[i][1]=((b[i] START >> index) & 1);

printf("first 64 bit\n");
for(i=63;i>=0;i--)
    printf("%d",x[i][0]);
printf("\n");


//for (bit =0; bit<64; bit++)
  //      x[bit][0]=(b[bit]START & 1);
     

for (dst = 0; dst < 64; dst++) {
		int src = DES_IP[dst ^ 0x20];
	x[dst][0]=x[src][0];
}

*/
//printf("Temp value..\n");
//for(i=0;i<64;i++)
 //   printf("%d",x[i][0]);
//printf("\n");



/*printf("last 64 bit\n");
for(i=31;i>=0;i--)
     printf("%d",x[i][1]);
printf("\n");

*/

//printf("Index in bs....%d\n",index);


//for(i=0;i<64;i++)

//printf("b values....%u..%u..%d\n",b[i][0],b[i][1],index);

	result = (b[0] START >> index) & 1;
	result |= ((b[1] START >> index) & 1) << 1;
	result |= ((b[2] START >> index) & 1) << 2;
	result |= ((b[3] START >> index) & 1) << 3;

//printf("hi..%d\n",result);

	if (count == 4) return result;


	result |= ((b[4] START >> index) & 1) << 4;
	result |= ((b[5] START >> index) & 1) << 5;
	result |= ((b[6] START >> index) & 1) << 6;
	result |= ((b[7] START >> index) & 1) << 7;
	if (count == 8) return result;

	result |= ((b[8] START >> index) & 1) << 8;
	result |= ((b[9] START >> index) & 1) << 9;
	result |= ((b[10] START >> index) & 1) << 10;
	result |= ((b[11] START >> index) & 1) << 11;
	if (count == 12) return result;

	result |= ((b[12] START >> index) & 1) << 12;
	result |= ((b[13] START >> index) & 1) << 13;
	result |= ((b[14] START >> index) & 1) << 14;
	result |= ((b[15] START >> index) & 1) << 15;
	if (count == 16) return result;

	result |= ((b[16] START >> index) & 1) << 16;
	result |= ((b[17] START >> index) & 1) << 17;
	result |= ((b[18] START >> index) & 1) << 18;
	result |= ((b[19] START >> index) & 1) << 19;

	return result;
}

/*
 * The trick I used here allows to compare one ciphertext against all the
 * DES_bs_crypt() outputs in just O(log2(ARCH_BITS)) operations, assuming
 * that DES_BS_VECTOR is 0 or 1. This routine isn't vectorized, yet.
 */
int DES_bs_cmp_all(ARCH_WORD *binary)
{
	ARCH_WORD value, mask;
	int bit;
#if DES_BS_VECTOR
	int depth;
#endif
	DES_bs_vector *b;

	for_each_depth() {
		value = binary[0];
		b = (DES_bs_vector *)&DES_bs_all.B[0] DEPTH;

		mask = b[0] START ^ -(value & 1);
		mask |= b[1] START ^ -((value >> 1) & 1);
		if (mask == ~(ARCH_WORD)0) goto next_depth;
		mask |= b[2] START ^ -((value >> 2) & 1);
		mask |= b[3] START ^ -((value >> 3) & 1);
		if (mask == ~(ARCH_WORD)0) goto next_depth;
		value >>= 4;
		b += 4;
		for (bit = 4; bit < 32; bit += 2) {
			mask |= b[0] START ^
				-(value & 1);
			if (mask == ~(ARCH_WORD)0) goto next_depth;
			mask |= b[1] START ^
				-((value >> 1) & 1);
			if (mask == ~(ARCH_WORD)0) goto next_depth;
			value >>= 2;
			b += 2;
		}

		return 1;
next_depth:
		;
	}

	return 0;
}

int DES_bs_cmp_one(ARCH_WORD *binary, int count, int index)
{
	int bit;
	DES_bs_vector *b;

	init_depth();
	b = (DES_bs_vector *)&DES_bs_all.B[0] DEPTH;


	for (bit = 0; bit < 31; bit++, b++)
		if (((b[0] START >> index) ^ (binary[0] >> bit)) & 1) return 0;

	for (; bit < count; bit++, b++)
		if (((b[0] START >> index) ^
			(binary[bit >> 5] >> (bit & 0x1F))) & 1) return 0;

	

        return 1;
}

int DES_bs_cmp_one_oracle(ARCH_WORD *binary, int count, int index)
{
	int bit;
	DES_bs_vector *b;

	init_depth();
	b = (DES_bs_vector *)&DES_bs_all.B[0] DEPTH;


	for (bit = 0; bit < 31; bit++, b++)
		if (((b[0] START >> index) ^ (binary[0] >> bit)) & 1) return 0;

	for (; bit < count; bit++, b++)
		if (((b[0] START >> index) ^
			(binary[bit >> 5] >> (bit & 0x1F))) & 1) return 0;


for_each_depth() {

	   for (bit = 0; bit <64; bit++){

                if(bit==0||bit==51)
		DES_bs_all.B[bit][0]=0;
          
                 else
                DES_bs_all.B[bit]DEPTH=0;
           }
          
}

        return 1;
}

unsigned char *decimal_to_text(unsigned long a)

{
int i,j=0;
int temp[64];
int parity[56];
for(i=63;i>=0;i--){
       temp[j]=(a>>i)&1;
       j++;
}
j=0;
for(i=0;i<64;i++){

  if(i==7||i==15||i==23||i==31||i==39||i==47||i==55||i==63){;}

  else{
   parity[j]=temp[i];
   j++; 
  }
}


j=0;
for(i=0;i<56;i=i+8){
//printf("hello..");
t[j]=(128*parity[i]+64*parity[i+1]+32*parity[i+2]+16*parity[i+3]+8*parity[i+4]+4*parity[i+5]+2*parity[i+6]+parity[i+7]);
j++;
}

return t;
}

Powered by blists - more mailing lists

Your e-mail address:

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