⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aestable.c

📁 usb透明加密驱动程序源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
void ExpandKey (uchar *key, uchar *expkey)
{
uchar tmp0, tmp1, tmp2, tmp3, tmp4;
unsigned idx;

	for( idx = 0; idx < Nk; idx++ ) {
		expkey[4*idx+0] = key[4 * idx + 0];
		expkey[4*idx+1] = key[4 * idx + 1];
		expkey[4*idx+2] = key[4 * idx + 2];
		expkey[4*idx+3] = key[4 * idx + 3];
	}

	for( idx = Nk; idx < Nb * (Nr + 1); idx++ ) {
		tmp0 = expkey[4*idx - 4];
		tmp1 = expkey[4*idx - 3];
		tmp2 = expkey[4*idx - 2];
		tmp3 = expkey[4*idx - 1];
		if( !(idx % Nk) ) {
			tmp4 = tmp3;
			tmp3 = Sbox[tmp0];
			tmp0 = Sbox[tmp1] ^ Rcon[idx/Nk];
			tmp1 = Sbox[tmp2];
			tmp2 = Sbox[tmp4];
		}

		//  convert from longs to bytes

		expkey[4*idx+0] = expkey[4*idx - 4*Nk + 0] ^ tmp0;
		expkey[4*idx+1] = expkey[4*idx - 4*Nk + 1] ^ tmp1;
		expkey[4*idx+2] = expkey[4*idx - 4*Nk + 2] ^ tmp2;
		expkey[4*idx+3] = expkey[4*idx - 4*Nk + 3] ^ tmp3;
	}
}

// encrypt one 128 bit block
void Encrypt (uchar *in, uchar *expkey, uchar *out)
{
unsigned round, idx;
uchar state[Nb * 4];

	for( idx = 0; idx < Nb; idx++ ) {
		state[4*idx+0] = *in++;
		state[4*idx+1] = *in++;
		state[4*idx+2] = *in++;
		state[4*idx+3] = *in++;
	}

	AddRoundKey ((unsigned *)state, (unsigned *)expkey);

	for( round = 1; round < Nr + 1; round++ ) {
		if( round < Nr )
			MixSubColumns (state);
		else
			ShiftRows (state);

		AddRoundKey ((unsigned *)state, (unsigned *)expkey + round * Nb);
	}

	for( idx = 0; idx < Nb; idx++ ) {
		*out++ = state[4*idx+0];
		*out++ = state[4*idx+1];
		*out++ = state[4*idx+2];
		*out++ = state[4*idx+3];
	}
}

void Decrypt (uchar *in, uchar *expkey, uchar *out)
{
unsigned idx, round;
uchar state[Nb * 4];

	for( idx = 0; idx < Nb; idx++ ) {
		state[4*idx+0] = *in++;
		state[4*idx+1] = *in++;
		state[4*idx+2] = *in++;
		state[4*idx+3] = *in++;
	}

	AddRoundKey ((unsigned *)state, (unsigned *)expkey + Nr * Nb);
	round = Nr;

	InvShiftRows(state);

	while( round-- )
	{
		AddRoundKey ((unsigned *)state, (unsigned *)expkey + round * Nb);
		if( round )
			InvMixSubColumns (state);
	} 

	for( idx = 0; idx < Nb; idx++ ) {
		*out++ = state[4*idx+0];
		*out++ = state[4*idx+1];
		*out++ = state[4*idx+2];
		*out++ = state[4*idx+3];
	}
}

/*
uchar in[16] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};

uchar key[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};

uchar out[16];

void encrypt (char *mykey, char *name)
{
uchar expkey[4 * Nb * (Nr + 1)];
FILE *fd = fopen (name, "rb");
int ch, idx = 0;

	strncpy (key, mykey, sizeof(key));
	ExpandKey (key, expkey);

	while( ch = getc(fd), ch != EOF ) {
		in[idx++] = ch;
		if( idx % 16 )
			continue;

		Encrypt (in, expkey, out);

		for( idx = 0; idx < 16; idx++ )
			putchar (out[idx]);
		idx = 0;
	}

	if( idx )
	  while( idx % 16 )
		in[idx++] = 0;
	else
	  return;

	Encrypt (in, expkey, out);

	for( idx = 0; idx < 16; idx++ )
		putchar (out[idx]);
}

uchar expkey[4 * Nb * (Nr + 1)];
void mrandom (int, char *);
unsigned xrandom (void);

int aescycles ()
{
__int64 start, end;
int t;

	do {
		rd_clock(&start);
		Encrypt (in, expkey, out);
		rd_clock (&end);
		t = end - start;
	} while( t<= 0 || t>= 4000);
	return t;
}

int bestx (int b, int loops)
{
int bestx = 0, bestxt = 0;
int x, xt, i, j;

	for( x = 0; x < 256; x++ ) {
		xt = 0;
		for( i = 0; i < loops; i++ ) {
			for( j = 0; j < 16; j++ )
				in[j] = xrandom() >> 16;
			in[b] = x;
			xt += aescycles(); xt += aescycles(); xt += aescycles();
			xt += aescycles(); xt += aescycles();
		}
		if( xt > bestxt )
			bestx = x, bestxt = xt;
	}
	return bestx;
}

int main (int argc, char *argv[])
{
	switch( argv[1][0] ) {
	case 'e': encrypt(argv[2], argv[3]); break;
	case 'd': decrypt(argv[2], argv[3]); break;

	}
}

/*
 * The package generates far better random numbers than a linear
 * congruential generator.  The random number generation technique
 * is a linear feedback shift register approach.  In this approach,
 * the least significant bit of all the numbers in the RandTbl table
 * will act as a linear feedback shift register, and will have period
 * of approximately 2^96 - 1.
 *
 */

#define RAND_order (7 * sizeof(unsigned))
#define RAND_size (96 * sizeof(unsigned))

uchar RandTbl[RAND_size + RAND_order];
int RandHead = 0;

/*
 * random: 	x**96 + x**7 + x**6 + x**4 + x**3 + x**2 + 1
 *
 * The basic operation is to add to the number at the head index
 * the XOR sum of the lower order terms in the polynomial.
 * Then the index is advanced to the next location cyclically
 * in the table.  The value returned is the sum generated.
 *
 */

unsigned xrandom ()
{
register unsigned fact;

	if( (RandHead -= sizeof(unsigned)) < 0 ) {
		RandHead = RAND_size - sizeof(unsigned);
		memcpy (RandTbl + RAND_size, RandTbl, RAND_order);
	}

	fact = *(unsigned *)(RandTbl + RandHead + 7 * sizeof(unsigned));
	fact ^= *(unsigned *)(RandTbl + RandHead + 6 * sizeof(unsigned));
	fact ^= *(unsigned *)(RandTbl + RandHead + 4 * sizeof(unsigned));
	fact ^= *(unsigned *)(RandTbl + RandHead + 3 * sizeof(unsigned));
	fact ^= *(unsigned *)(RandTbl + RandHead + 2 * sizeof(unsigned));
	return *(unsigned *)(RandTbl + RandHead) += fact;
}

/*
 * mrandom:
 * 		Initialize the random number generator based on the given seed.
 *
 */

void mrandom (int len, char *ptr)
{
unsigned short rand = *ptr;
int idx, bit = len * 4;

	memset (RandTbl, 0, sizeof(RandTbl));
	RandHead = 0;

	while( rand *= 20077, rand += 11, bit-- )
		if( ptr[bit >> 2] & (1 << (bit & 3)) )
			for (idx = 0; idx < 5; idx++) {
				rand *= 20077, rand += 11;
				RandTbl[rand % 96 << 2] ^= 1;
			}

	for( idx = 0; idx < 96 * 63; idx++ )
		xrandom ();
}

//
//appended by boywhp 08/09/18
//
VOID
EncryptBuf_128AES(
				  IN OUT PBYTE Buf,
				  IN ULONG Length
				  )
{
	BYTE expkey[4 * Nb * (Nr + 1)];
	BYTE srcBytes[16];
	UINT i;
	
	ASSERT(Buf && ((Length & 0xf)==0));
	
	//for (i=0; i<Length; i++)
	//	Buf[i] ^= 0xff;
	
	
	KdPrint(("Encrypt_128AES:%08x %d Bytes\n", Buf, Length));
	
	ExpandKey(Globals.Key, expkey);
	
	for (i=0; i<Length; i+=16)
	{
		memcpy(srcBytes, Buf+i, 16);
		Encrypt(srcBytes, expkey, Buf+i);
	}
	
}

VOID
DecryptBuf_128AES(
				  IN OUT PBYTE Buf,
				  IN ULONG Length
				  )
{
	BYTE expkey[4 * Nb * (Nr + 1)];
	BYTE srcBytes[16];
	UINT i;
	
	ASSERT(Buf && ((Length & 0xf)==0));
	
	//for (i=0; i<Length; i++)
	//	Buf[i] ^= 0xff;
	
	KdPrint(("Decrypt_128AES:%08x %d Bytes\n", Buf, Length));
	
	ExpandKey(Globals.Key, expkey);
	
	for (i=0; i<Length; i+=16)
	{
		memcpy(srcBytes, Buf+i, 16);
		Decrypt(srcBytes, expkey, Buf+i);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -