#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <net/if_arp.h>
#include "md5.h"
#include "dhcp_plus.h"
//extern unsigned char first_genkey;
//extern unsigned char key1[2];
unsigned char first_genkey;
unsigned char key1[2];
//将明文rawcmd加密到crypto_buf，80bit的key事先放在crypto_buf的前10个byte
void encrypt_suboption( unsigned char *rawcmd, int rawcmd_len, unsigned char *crypto_buf)
{
	int cnt; 
	unsigned int const_val = 0x7b;
	unsigned char c_mw, c_enc;
	for( cnt=0; cnt<rawcmd_len; cnt++)
	{
		c_mw = rawcmd[cnt];
		c_enc = crypto_buf[cnt%10];
		c_enc^= (const_val * 5)&0xff;
		c_enc^= cnt*7;
		c_enc+= c_mw;
		crypto_buf[10+cnt] = c_enc;
		const_val^=(c_mw^(c_enc + 0x346d));
	}
}
//解密
int decrypt_data(unsigned char *cryptob, int clen, unsigned char *decryptob)
{
	int cnt;
	unsigned char cc, ck;
	unsigned int const_val = 0x7b;
	for( cnt=0; cnt<clen-10; cnt++)
	{
		cc = cryptob[10+cnt];
		ck = cryptob[cnt%10];
		ck^=((const_val*5)&0xff);		
		ck^=cnt*7;
		cc-=ck;
		decryptob[cnt]=cc;
		const_val^=(cc^(cryptob[10+cnt]+0x346d));
	}
	//printb("decrypt data", decryptob, cnt);
	return cnt;
}
//打印buf
void printb(unsigned char *prompt, unsigned char *buf, int len)
{
	int i;
	unsigned char temp_str[200], *temp_ptr;
	memset(temp_str, 0, sizeof(temp_str));
	temp_ptr = temp_str;
	for( i = 0;  i<len; i++)
	{
		sprintf(temp_ptr, "%02x ", *(buf+i));
		temp_ptr += 3;
	}
	*temp_ptr = 0;
	//DBG_PRINTF("%s: %s\n", prompt, temp_str);
}

static unsigned char init_key[14]={0xc9,0x23,0x75,0x18,0xd7,0xe2,0x12,0x35,0x29,0x2b,0xec,0xb6,0x23,0x19};
//根据净荷部分(str)生成80位KEY

void genkey(unsigned char *key, unsigned char *str, unsigned int len)
{
	unsigned char i, j, n;
	unsigned char rand_val[2];
	unsigned char key_buf[16], str_buf[16];
	srand((int)time(0));
	if( first_genkey == 1)
	{
		rand_val[0] = (unsigned char)rand();
		rand_val[1] = (unsigned char)rand();
		key1[0] = rand_val[0];
		key1[1] = rand_val[1];
		first_genkey = 0;
	}
	else
	{
		rand_val[0] = key1[0];
		rand_val[1] = key1[1];
	}
	key_buf[0] = rand_val[0];
	key_buf[1] = rand_val[1];
	memcpy(key_buf+2, init_key, 14);
	n = len / 16;
	for( i = 0; i < n; i++)
	{
		encry_simple_two_128bit(key_buf, str + i*16);
	}
	n = 16 - (len % 16);
	if( n != 16)
	{
		memcpy(str_buf, str+ i*16, 16 - n);
		//printf("n=%d\n", n);
#if	0
		if	( n > 6)	
		{
			for( j = 0;  j< n; j++)
				str_buf[16-n+j] = 0xa;
		}
		else
#endif
		{
			for( j = 0; j < n; j++)
				str_buf[16-n+j] = n;
		}
		encry_simple_two_128bit(key_buf, str_buf);
	}
	for( i = 0; i < 8; i++)
	{
		key[2+i] = key_buf[i] ^ key_buf[i+8];		
	}
	key[0] = rand_val[0];
	key[1] = rand_val[1];
}
struct buf_obj
{
	unsigned char key_local[16];
	unsigned char data_local[16];
	unsigned char trans_data[16];
};
//中间KEY的生成，用于80bit的key的生成。由16byte的key和16byte的data生成16byte的key
void encry_simple_two_128bit(unsigned char *key, unsigned char *data)
{
	int i, j;
	struct buf_obj local_buf;
	unsigned char *ptr;
	unsigned char a,c ;
	unsigned char d=0;
//	unsigned char *b=(unsigned char *)&local_buf;
	memcpy(local_buf.key_local, key, 16);
	memcpy(local_buf.data_local, data, 16);
	for( i = 0;  i< 16; i++)
	{
		local_buf.trans_data[i] = key[i]^data[i];
	}
	for( i = 0; i < 20; i++)
	{
		ptr = (char*)&local_buf;
		for( j = 0; j < 48; j++)
		{
			c = ptr[j];
			a = ( (c>>2) & (c<<2) )^c;
			a^=(unsigned char)(d&0xff);
			a^=0x75;
			ptr[j] = a;
			a += 0x85;
			d = a;
		}


		d += 0x3b;
	}
	memcpy( key, local_buf.key_local, 16);
	//printb("output", key, 16);
}