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

📄 beeo_dh.c

📁 基于beeo标准的数据加解密算法
💻 C
字号:
// DH.c : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string.h>
#include <stdio.h>

#include "beeo_Define.h"
#include "beeo/beeo_Basic.h"

#ifndef uIn64	//Not support 64b operation

typedef struct tagBeeoDhProcessor
{
	uIn16*	wPattern;
	uIn32	dwFlag;
	iIn16	nWords;
	iIn16	xx;
	uIn16*	wBase;
	uIn16*	wFeature;
	uIn16*	wTmp;
} BEEO_DH_PROCESSOR;

//初始化一个长度为'bits'位的DH计算机,模数为pattern
//返回一个无符号长整形标志
//bits应该为32的倍数
uIn32 PASCAL dh_Alloc(void* pattern, int bytes)
{
	BEEO_DH_PROCESSOR* dhp;
	iIn16 i;
	
	if(!pattern) return 0;
	if(bytes<4) return 0;
	if(bytes>32768) return 0;
	
	dhp = (BEEO_DH_PROCESSOR*)sl_malloc(sizeof(BEEO_DH_PROCESSOR));
	if(!dhp) return 0;
	
	memset(dhp, 0, sizeof(BEEO_DH_PROCESSOR));
	dhp->dwFlag=BEEO_DH_FLAG;
	dhp->nWords=bytes/4;
	dhp->wPattern=(uIn16*)sl_malloc(dhp->nWords*2+4);
	dhp->wBase=(uIn16*)sl_malloc(dhp->nWords*2+4);
	dhp->wFeature=(uIn16*)sl_malloc(dhp->nWords*2+4);
	dhp->wTmp=(uIn16*)sl_malloc((dhp->nWords*2+4)*2);
	if(!(dhp->wPattern&&dhp->wBase&&dhp->wFeature&&dhp->wTmp))
	{
		dh_Free((uIn32)dhp);
		return 0;
	}
	memcpy(dhp->wPattern, pattern, dhp->nWords<<1);
	for(i=0;i<dhp->nWords;i++)
	{//转换成双字节,并转换成本地字节顺序
		dhp->wPattern[i]=ntohs(dhp->wPattern[i]);
	}
	return (uIn32)dhp;
}

/******************************************************************************
描述:	
	把常数数组P乘以一个32bits数,结果保存成为nWords+1个32bit数据。
输入数据:UInt64 tmp64, 有效数位32bit
输出数据:dwData数组
返回值:  1
******************************************************************************/
int dh_Multi_16b(uIn16 * wData, uIn16 x, iIn16 nWords, uIn16* wOut)
{
	uIn32 t1,t2;

	t2 = 0;
	for(;nWords>0;nWords--)
	{
		t1 = wData[nWords-1];
		t1 *= x;
		t1 += t2;
		wOut[nWords] = (uIn16)t1;
		t2 = t1>>16;
	}
	wOut[0] = (uIn16)t2;

	return 1;
}

/******************************************************************************
描述:	
	比较两个数组A、B,数组结构为25个32bits无符号型整数,高位在先,不足位补0x00000000。
返回值:  return 1 if(A>B) , return -1 if(A<B), return 0 else
******************************************************************************/
int dh_Compare_16(uIn16* dwA, uIn16* dwB, iIn16 nWords)
{
	iIn16 i;
	for(i=0;i<(nWords+1);i++)
	{
		if(dwA[i]>dwB[i]) return 1;
		if(dwA[i]<dwB[i]) return -1;
	}
	return 0;
}

/******************************************************************************
描述:	
	对两个数组A、B相减,数组结构为25个32bits无符号型整数,高位在先,不足位补0x00000000。
	输入参数中,保证A>B,所以结果不会溢出
返回值: 1
******************************************************************************/
int dh_Sub_16(uIn16* dwA, uIn16* dwB, iIn16 nWords)
{
	int i;
	iIn32 t1;
	
	t1 = 0;
	for(i=nWords; i>=0; i--)
	{
		t1 += dwA[i] - dwB[i];
		dwA[i] = (uIn16)t1;
		t1  >>= 16;
	}
	return 1;
}

/******************************************************************************
描述:	
	对两个数组A、B相减,数组末尾对齐
	数组A结构为25个32bits无符号型整数,高位在先,不足位补0x00000000。
	数组B结构为24个32bits无符号型整数,高位在先,不足位补0x00000000。
	输入参数中,保证A>B,所以结果不会溢出
返回值: 1
******************************************************************************/
int dh_Sub_1_16(uIn16* dwA, uIn16* dwB, iIn16 nWords)
{
	int i;
	iIn32 t1;
	
	t1 = 0;
	for(i=nWords; i>0; i--)
	{
		t1 += dwA[i]-dwB[i-1];
		dwA[i] = (uIn16)t1;
		t1 >>= 16;
	}
	t1 += dwA[i];
	dwA[i] = (uIn16)t1;
	t1 >>= 16;
	return 1;
}

/******************************************************************************
描述:	
	对一个长度为(768+768)=1536(bits)的数据取模。
输入数据:dwData数组,结构为50个32bits无符号型整数,高位在先,不足位补0x00000000。
			由于最大数也只有48个整数,输入数据至少有2个先导数据0x00000000,这是为了计算的方便。
输出数据:直接保存在dwData数组中,结构与输入数据相同
返回值:  1
******************************************************************************/
int dh_Mod_16(BEEO_DH_PROCESSOR* dhp)
{
	iIn16 i;
	uIn32 tmp32;
	uIn16* wData;
	uIn16* wQ;
	
	wData=dhp->wTmp;
	wQ=dhp->wFeature;
	tmp32=0;
	for(i=0;i<(dhp->nWords+3);i++)
	{
		tmp32 <<= 16;
		tmp32 += wData[i];
		tmp32 /= dhp->wPattern[0];
		if(tmp32)
		{
			//试商
			dh_Multi_16b(dhp->wPattern, (uIn16)tmp32, dhp->nWords, wQ);
			while(tmp32 > 0)
			{
				if(dh_Compare_16(&(wData[i-1]),wQ, dhp->nWords)<0)
				{//失败,减1以后继续
					dh_Sub_1_16(wQ, dhp->wPattern, dhp->nWords);
					tmp32 --;
					continue;
				}
				dh_Sub_16(&(wData[i-1]),wQ, dhp->nWords);
				break;
			}
		}
		tmp32 = wData[i];
	}
	return 1;
}

void dh_Multiply_16(BEEO_DH_PROCESSOR* dhp)
{
	uIn16 *pd, *pTmp, *pBase, *pFeature;
	iIn16 i,j;
	uIn16 t1;
	uIn32 t2, t3;
	
	pTmp = dhp->wTmp;
	pBase = dhp->wBase;
	pFeature = dhp->wFeature;
	memset(pTmp, 0, (dhp->nWords+1)<<2);
	for(i=dhp->nWords; i>0; i--)
	{
		pd = pTmp+dhp->nWords+1-(dhp->nWords-i);
		t1 = pBase[i];
		t3 = 0;
		for(j=dhp->nWords;j>0; j--)
		{
			t2 = pFeature[j];
			t2 *= t1;
			t2 += t3;
			t2 += pd[j];
			t3 = t2>>16;
			pd[j] = (uIn16)t2;
		}
		pd[0] += (uIn16)t3;
	}
	
	dh_Mod_16(dhp);
	memcpy(pFeature, pTmp+dhp->nWords+1, (dhp->nWords+1)<<1);
}

//以base为底数、exponent为指数求DH特征值
// feature = ( base exp exponent ) mod pattern
//base和feature 的长度均为初始化时指定的长度,高位在先,不足补0
int PASCAL dh_GetFeature(uIn32 dhID, void* base, uIn32 exponent, void* feature)
{
	BEEO_DH_PROCESSOR* dhp;
	iIn16 i;
	uIn16* pOut;

	dhp=(BEEO_DH_PROCESSOR*)dhID;
	if(!dhp) return 0;
	if(dhp->dwFlag!=BEEO_DH_FLAG) return 0;
	dhp->wBase[0]=0;
	memcpy(dhp->wBase+1, base, dhp->nWords<<1);
	for(i=1;i<=dhp->nWords;i++) dhp->wBase[i]=ntohs(dhp->wBase[i]);
	memcpy(dhp->wFeature, dhp->wBase,(dhp->nWords+1)<<1);
	while(exponent>1)
	{
		dh_Multiply_16(dhp);
		exponent --;
	}
	pOut = (uIn16*)feature;
	for(i=0;i<dhp->nWords;i++)
	{
		pOut[i]=ntohs(dhp->wFeature[i+1]);
	}
	return 1;
}




#else



typedef struct tagBeeoDhProcessor
{
	uIn32*	wPattern;
	uIn32	dwFlag;
	iIn16	nWords;
	iIn16	xx;
	uIn32*	wBase;
	uIn32*	wFeature;
	uIn32*	wTmp;
} BEEO_DH_PROCESSOR;

//初始化一个长度为'bits'位的DH计算机,模数为pattern
//返回一个无符号长整形标志
//bytes应该为4的倍数
uIn32 PASCAL dh_Alloc(void* pattern, int bytes)
{
	BEEO_DH_PROCESSOR* dhp;
	iIn16 i;
	
	if(!pattern) return 0;
	if(bytes<4) return 0;
	if(bytes>32768) return 0;
	
	dhp = (BEEO_DH_PROCESSOR*)sl_malloc(sizeof(BEEO_DH_PROCESSOR));
	if(!dhp) return 0;
	
	memset(dhp, 0, sizeof(BEEO_DH_PROCESSOR));
	dhp->dwFlag=BEEO_DH_FLAG;
	dhp->nWords=bytes/4;
	dhp->wPattern=(uIn32*)sl_malloc(dhp->nWords*4+4);
	dhp->wBase=(uIn32*)sl_malloc(dhp->nWords*4+4);
	dhp->wFeature=(uIn32*)sl_malloc(dhp->nWords*4+4);
	dhp->wTmp=(uIn32*)sl_malloc((dhp->nWords*4+4)*2);
	if(!(dhp->wPattern&&dhp->wBase&&dhp->wFeature&&dhp->wTmp))
	{
		dh_Free((uIn32)dhp);
		return 0;
	}
	memcpy(dhp->wPattern, pattern, dhp->nWords<<2);
	for(i=0;i<dhp->nWords;i++)
	{//转换成四字节,并转换成本地字节顺序
		dhp->wPattern[i]=bs_ntohl(dhp->wPattern[i]);
	}
	return (uIn32)dhp;
}

/******************************************************************************
描述:	
	把常数数组P乘以一个32bits数,结果保存成为nWords+1个32bit数据。
输入数据:UInt64 tmp64, 有效数位32bit
输出数据:dwData数组
返回值:  1
******************************************************************************/
int dh_Multi_32b(uIn32 * wData, uIn32 x, iIn16 nWords, uIn32* wOut)
{
	uIn64 t1,t2;

	t2 = 0;
	for(;nWords>0;nWords--)
	{
		t1 = wData[nWords-1];
		t1 *= x;
		t1 += t2;
		wOut[nWords] = (uIn32)t1;
		t2 = t1>>32;
	}
	wOut[0] = (uIn32)t2;

	return 1;
}

/******************************************************************************
描述:	
	比较两个数组A、B,数组结构为25个32bits无符号型整数,高位在先,不足位补0x00000000。
返回值:  return 1 if(A>B) , return -1 if(A<B), return 0 else
******************************************************************************/
int dh_Compare_32(uIn32* dwA, uIn32* dwB, iIn16 nWords)
{
	iIn16 i;
	for(i=0;i<(nWords+1);i++)
	{
		if(dwA[i]>dwB[i]) return 1;
		if(dwA[i]<dwB[i]) return -1;
	}
	return 0;
}

/******************************************************************************
描述:	
	对两个数组A、B相减,数组结构为25个32bits无符号型整数,高位在先,不足位补0x00000000。
	输入参数中,保证A>B,所以结果不会溢出
返回值: 1
******************************************************************************/
int dh_Sub_32(uIn32* dwA, uIn32* dwB, iIn16 nWords)
{
	int i;
	uIn64 t1;
	
	t1 = 1;
	for(i=nWords; i>=0; i--)
	{
		t1 += dwA[i];
		t1 += ~dwB[i];
		dwA[i] = (uIn32)t1;
		t1 >>= 32;
	}
	return 1;
}

/******************************************************************************
描述:	
	对两个数组A、B相减,数组末尾对齐
	数组A结构为25个32bits无符号型整数,高位在先,不足位补0x00000000。
	数组B结构为24个32bits无符号型整数,高位在先,不足位补0x00000000。
	输入参数中,保证A>B,所以结果不会溢出
返回值: 1
******************************************************************************/
int dh_Sub_1_32(uIn32* dwA, uIn32* dwB, iIn16 nWords)
{
	int i;
	uIn64 t1;
	
	t1 = 1;
	for(i=nWords; i>0; i--)
	{
		t1 += dwA[i];
		t1 += ~dwB[i-1];
		dwA[i] = (uIn32)t1;
		t1 >>= 32;
	}
	t1 += dwA[i];
	t1 += ~0;
	dwA[i] = (uIn32)t1;
	t1 >>= 32;
	return 1;
}

/******************************************************************************
描述:	
	对一个长度为(768+768)=1536(bits)的数据取模。
输入数据:dwData数组,结构为50个32bits无符号型整数,高位在先,不足位补0x00000000。
			由于最大数也只有48个整数,输入数据至少有2个先导数据0x00000000,这是为了计算的方便。
输出数据:直接保存在dwData数组中,结构与输入数据相同
返回值:  1
******************************************************************************/
int dh_Mod_32(BEEO_DH_PROCESSOR* dhp)
{
	iIn16 i;
	uIn64 tmp64;
	uIn32* wData;
	uIn32* wQ;
	
	wData=dhp->wTmp;
	wQ=dhp->wFeature;
	tmp64=0;
	for(i=0;i<(dhp->nWords+3);i++)
	{
		tmp64 <<= 32;
		tmp64 += wData[i];
		tmp64 /= dhp->wPattern[0];
		if(tmp64)
		{
			//试商
			dh_Multi_32b(dhp->wPattern, (uIn32)tmp64, dhp->nWords, wQ);
			while(tmp64 > 0)
			{
				if(dh_Compare_32(&(wData[i-1]),wQ, dhp->nWords)<0)
				{//失败,减1以后继续
					dh_Sub_1_32(wQ, dhp->wPattern, dhp->nWords);
					tmp64 --;
					continue;
				}
				dh_Sub_32(&(wData[i-1]),wQ, dhp->nWords);
				break;
			}
		}
		tmp64 = wData[i];
	}
	return 1;
}

void dh_Multiply_32(BEEO_DH_PROCESSOR* dhp)
{
	uIn32 *pd, *pTmp, *pBase, *pFeature;
	iIn16 i,j;
	uIn32 t1;
	uIn64 t2, t3;
	
	pTmp = dhp->wTmp;
	pBase = dhp->wBase;
	pFeature = dhp->wFeature;
	memset(pTmp, 0, (dhp->nWords+1)<<3);
	for(i=dhp->nWords; i>0; i--)
	{
		pd = pTmp+dhp->nWords+1-(dhp->nWords-i);
		t1 = pBase[i];
		t3 = 0;
		for(j=dhp->nWords;j>0; j--)
		{
			t2 = pFeature[j];
			t2 *= t1;
			t2 += t3;
			t2 += pd[j];
			t3 = t2>>32;
			pd[j] = (uIn32)t2;
		}
		pd[0] += (uIn32)t3;
	}
	
	dh_Mod_32(dhp);
	memcpy(pFeature, pTmp+dhp->nWords+1, (dhp->nWords+1)<<2);
}

//以base为底数、exponent为指数求DH特征值
// feature = ( base exp exponent ) mod pattern
//base和feature 的长度均为初始化时指定的长度,高位在先,不足补0
int PASCAL dh_GetFeature(uIn32 dhID, void* base, uIn32 exponent, void* feature)
{
	BEEO_DH_PROCESSOR* dhp;
	iIn16 i;
	uIn32* pOut;
	
	dhp=(BEEO_DH_PROCESSOR*)dhID;
	if(!dhp) return 0;
	if(dhp->dwFlag!=BEEO_DH_FLAG) return 0;
	dhp->wBase[0]=0;
	memcpy(dhp->wBase+1, base, dhp->nWords<<2);
	for(i=1;i<=dhp->nWords;i++)
		dhp->wBase[i]=bs_ntohl(dhp->wBase[i]);
	memcpy(dhp->wFeature, dhp->wBase,(dhp->nWords+1)<<2);
	while(exponent>1)
	{
		dh_Multiply_32(dhp);
		exponent --;
	}
	pOut = (uIn32*)feature;
	for(i=0;i<dhp->nWords;i++)
	{
		pOut[i]=bs_ntohl(dhp->wFeature[i+1]);
	}
	return 1;
}



#endif



void PASCAL dh_Free(uIn32 dhID)
{
	BEEO_DH_PROCESSOR* dhp;

	dhp=(BEEO_DH_PROCESSOR*)dhID;
	if(!dhp) return;
	if(dhp->dwFlag!=BEEO_DH_FLAG) return;
	if(dhp->wPattern) sl_free(dhp->wPattern);
	if(dhp->wFeature) sl_free(dhp->wFeature);
	if(dhp->wBase)	  sl_free(dhp->wBase);
	if(dhp->wTmp)	  sl_free(dhp->wTmp);
	sl_free(dhp);
}

uIn16 PASCAL dh_ShortPower(uIn16 base, uIn16 exponent, uIn16 pattern)
{
	uIn32 tmp;

	tmp = base;
	while(exponent>1)
	{
		tmp *= base;
		tmp %= pattern;
		exponent --;
	}

	return (uIn16)tmp;
}

int PASCAL huge_HEX2String(void* h, int bytes, char* string, int bNeedLeadZero)
{
	int i,len=0;
	unsigned char* hex=(unsigned char*)h;
	unsigned char ch;

	for(i=0;i<bytes;i++)
	{
		ch = hex[i];
		if((!len)&&(!bNeedLeadZero))
		{
			if(!ch) continue;
			if(ch&0x80)
			{
				if(!i) len += sprintf(string, "%02X", ch);
				else   len += sprintf(string, "%03X", ch);
			}
			else len += sprintf(string, "%02X", ch);
			continue;
		}
		len += sprintf(string+len, "%02X", ch);
	}
	return len;
}

unsigned char PASCAL huge_String2AHex(char ch)
{
	if(ch<'0') return 0;
	if(ch<='9') return (unsigned char)(ch-'0');
	if(ch<'A') return 0;
	if(ch<='F') return (unsigned char)(ch-'A'+0x0a);
	if(ch<'a') return 0;
	if(ch<'f') return (unsigned char)(ch-'a'+0x0a);
	return 0;
}

void PASCAL huge_String2HEX(char* string, int stringLength, void* h, int bytes)
{
	unsigned char* hex=(unsigned char*)h;
	if(stringLength<(bytes<<1))
	{
		memset(hex, 0, bytes-((stringLength+1)>>1));
		hex += bytes-((stringLength+1)>>1);
	}
	else if(stringLength>(bytes<<1))
		stringLength=bytes<<1;

	if(stringLength&0x01)
	{
		hex[0]=huge_String2AHex(string[0]);
		string++;
		stringLength--;
		hex++;
	}
	
	while(stringLength)
	{
		hex[0]=huge_String2AHex(string[0]);
		hex[0]<<=4;
		hex[0]|=huge_String2AHex(string[1]);
		string+=2;
		stringLength-=2;
		hex++;
	}
}

⌨️ 快捷键说明

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