PBOCcmd.cpp 6.75 KB
/* 
 *  Copyright (c) 2011,  广州江南科友union  
 *  All rights reserved. 
 *  文件名称: PBOCcmd.cpp
 *  摘 要:    实现应用加密机PBOC指令接口  
 *  当前版本: 1.0,  编写者: 杨武,修改时间: 2011-11-22 修改内容: 创建
 *  
 */
#include "stdafx.h"
extern "C" 
{
	//计算PBOC MAC/TAC(B010)
	//输入参数:indexOfKey 密钥索引
	//输入参数:keyValByMK 密钥值
	//输入参数:typeOfFilled  填充方式
	//输入参数:macAlgo MAC算法
	//输入参数:lenOfMac 返回MAC长度位数,一个字节=2位
	//输入参数:disperseCount 离散次数
	//输入参数:disperseData 离散数据
	//输入参数:lenOfMacData 长度
	//输入参数:iniVec
	//输入参数:macData
	//输出数据:mac
	//返回值: <0:失败;>0成功 mac 长度
	_declspec(dllexport) int union_syj1001_genPBOCMac(char* hsmIp,int hsmPort,int timeout,int isCfg,int hsmLenOfMsgHeader,int isLenOfHsmMsg,int clientSocket,char*indexOfKey,char*keyValByMK,char*typeOfFilled,char*macAlgo,int lenOfMac,int disperseCount,char *disperseData,int lenOfMacData,char*iniVec,char*macData,char*mac)
	{
		int oldClientSocket = -1;
		oldClientSocket = clientSocket;
		int tmpLen = 0;
		if(isCfg==1)
		{
			CONFIG *cfg;
			cfg = (struct CONFIG*)malloc(sizeof(struct CONFIG));
			getConfig(cfg);
			if(cfg!=NULL)
			{
				memcpy(hsmIp,cfg->ip,strlen(cfg->ip));
				hsmPort = cfg->port;
				timeout = cfg->timeout;
				hsmLenOfMsgHeader = cfg->hsmLenOfMsgHeader;
				isLenOfHsmMsg = cfg->isLenOfHsmMsg;
				free(cfg);
			}
		}
		if(clientSocket==-1)
		{
			clientSocket = socketConnect(hsmIp,hsmPort);
		}
		if(clientSocket>0)
		{
			char outData[1024] = {0};
			char inData[1024] = {0};
			char tmp[10] ={0};
			int  lenOfMsg = 0;
			//判断是否需要添加两位消息长度
			if(1==isLenOfHsmMsg)
			{
				lenOfMsg = 2;
			}
			tmpLen += lenOfMsg;
			//添加消息头
			my_strcat(inData,(char*)(LPCTSTR)GetRandStr(hsmLenOfMsgHeader),tmpLen,hsmLenOfMsgHeader);
			//添加命令头
			my_strcat(inData,"B010",tmpLen,4);
			//添加密钥索引
			my_strcat(inData,indexOfKey,tmpLen,4);
			if(strcmp(indexOfKey,"FFFF")==0)
			{
				//添加密钥长度
				int keyLen = strlen(keyValByMK);
				memset(tmp,0,sizeof(tmp));
				lenToChar(keyLen,tmp);
				my_strcat(inData,tmp,tmpLen,2);
				//添加密钥值
				my_strcat(inData,keyValByMK,tmpLen,keyLen);
			}
			//填充方式
			my_strcat(inData,typeOfFilled,tmpLen,2);
			//添加mac算法
			my_strcat(inData,macAlgo,tmpLen,2);
			//返回MAC长度字节数
			memset(tmp,0,sizeof(tmp));
			IntToHexChar(lenOfMac/2,4,tmp);
			my_strcat(inData,tmp,tmpLen,4);

			//离散次数
			memset(tmp,0,sizeof(tmp));
			IntToChar(disperseCount,4,tmp);
			my_strcat(inData,tmp,tmpLen,4);
			//离散数据
			my_strcat(inData,disperseData,tmpLen,16*disperseCount);
			//数据字节长度
			memset(tmp,0,sizeof(tmp));
			IntToHexChar(lenOfMacData/2 ,4,tmp);
			my_strcat(inData,tmp,tmpLen,4);
			//IV
			my_strcat(inData,iniVec,tmpLen,16);
			//MAC数据
			my_strcat(inData,macData,tmpLen,lenOfMacData-16);


			//在报文体内增加消息长度
			if(1==isLenOfHsmMsg)
			{
				int lenOfReport = tmpLen-lenOfMsg;
				inData[0] = lenOfReport/256;
				inData[1] = lenOfReport%256;
			}
			int outlen = socketHandle(clientSocket,inData, tmpLen,outData,timeout);
			if(outlen>0)
			{
				//断开短连接
				if(oldClientSocket == -1)
				{
					socketDisconnect(clientSocket);
				}
				memset(tmp,0,sizeof(tmp));
				//获取错误码
				my_strncut(tmp,outData,4+hsmLenOfMsgHeader+lenOfMsg,2);
				if(strcmp(tmp,"00")==0)
				{
					my_strncut(mac,outData,4+hsmLenOfMsgHeader+lenOfMsg+2,lenOfMac);
					return lenOfMac;
				}
				else
				{
					return revValue(tmp);
				}
			}
			else
			{
				return unionSocketErr;
			}

		}
		else
		{
			return clientSocket;
		}

	}
	
	//验证PBOC MAC/TAC(B012)
	//输入参数:indexOfKey 密钥索引
	//输入参数:keyValByMK 密钥值
	//输入参数:typeOfFilled  填充方式
	//输入参数:macAlgo MAC算法
	//输入参数:disperseCount 离散次数
	//输入参数:disperseData 离散数据
	//输入参数:lenOfMac 长度 位数
	//输入参数:mac
	//输入参数:lenOfMacData 长度
	//输入参数:iniVec
	//输入参数:macData
	//返回值: <0:失败;=0成功
	_declspec(dllexport) int union_syj1001_verifyPBOCMac(char* hsmIp,int hsmPort,int timeout,int isCfg,int hsmLenOfMsgHeader,int isLenOfHsmMsg,int clientSocket,char*indexOfKey,char*keyValByMK,char*typeOfFilled,char*macAlgo,int disperseCount,char*disperseData,int lenOfMac,char*mac,int lenOfMacData,char*iniVec,char*macData)
	{
		int oldClientSocket = -1;
		oldClientSocket = clientSocket;
		int tmpLen = 0;
		if(isCfg==1)
		{
			CONFIG *cfg;
			cfg = (struct CONFIG*)malloc(sizeof(struct CONFIG));
			getConfig(cfg);
			if(cfg!=NULL)
			{
				memcpy(hsmIp,cfg->ip,strlen(cfg->ip));
				hsmPort = cfg->port;
				timeout = cfg->timeout;
				hsmLenOfMsgHeader = cfg->hsmLenOfMsgHeader;
				isLenOfHsmMsg = cfg->isLenOfHsmMsg;
				free(cfg);
			}
		}
		if(clientSocket==-1)
		{
			clientSocket = socketConnect(hsmIp,hsmPort);
		}
		if(clientSocket>0)
		{
			char outData[1024] = {0};
			char inData[1024] = {0};
			char tmp[10] ={0};
			int  lenOfMsg = 0;
			//判断是否需要添加两位消息长度
			if(1==isLenOfHsmMsg)
			{
				lenOfMsg = 2;
			}
			tmpLen += lenOfMsg;
			//添加消息头
			my_strcat(inData,(char*)(LPCTSTR)GetRandStr(hsmLenOfMsgHeader),tmpLen,hsmLenOfMsgHeader);
			//添加命令头
			my_strcat(inData,"B012",tmpLen,4);
			//添加密钥索引
			my_strcat(inData,indexOfKey,tmpLen,4);
			if(strcmp(indexOfKey,"FFFF")==0)
			{
				//添加密钥长度
				int keyLen = strlen(keyValByMK);
				memset(tmp,0,sizeof(tmp));
				lenToChar(keyLen,tmp);
				my_strcat(inData,tmp,tmpLen,2);
				//添加密钥值
				my_strcat(inData,keyValByMK,tmpLen,keyLen);
			}
			//填充方式
			my_strcat(inData,typeOfFilled,tmpLen,2);
			//添加mac算法
			my_strcat(inData,macAlgo,tmpLen,2);
			//离散次数
			memset(tmp,0,sizeof(tmp));
			IntToChar(disperseCount,4,tmp);
			my_strcat(inData,tmp,tmpLen,4);
			//离散数据
			my_strcat(inData,disperseData,tmpLen,16*disperseCount);
			//MAC长度
			memset(tmp,0,sizeof(tmp));
			IntToHexChar(lenOfMac/2,4,tmp);
			my_strcat(inData,tmp,tmpLen,4);
			//mac
            my_strcat(inData,mac,tmpLen,lenOfMac);
			
			//数据长度
			memset(tmp,0,sizeof(tmp));
			IntToHexChar(lenOfMacData/2 ,4,tmp);
			my_strcat(inData,tmp,tmpLen,4);
			//IV
			my_strcat(inData,iniVec,tmpLen,16);
			//MAC数据
			my_strcat(inData,macData,tmpLen,lenOfMacData-16);


			//在报文体内增加消息长度
			if(1==isLenOfHsmMsg)
			{
				int lenOfReport = tmpLen-lenOfMsg;
				inData[0] = lenOfReport/256;
				inData[1] = lenOfReport%256;
			}
			int outlen = socketHandle(clientSocket,inData,tmpLen,outData,timeout);
			if(outlen>0)
			{
				//断开短连接
				if(oldClientSocket == -1)
				{
					socketDisconnect(clientSocket);
				}
				memset(tmp,0,sizeof(tmp));
				//获取错误码
				my_strncut(tmp,outData,4+hsmLenOfMsgHeader+lenOfMsg,2);
				if(strcmp(tmp,"00")==0)
				{
					return 0;
				}
				else
				{
					return revValue(tmp);
				}
			}
			else
			{
				return unionSocketErr;
			}

		}
		else
		{
			return clientSocket;
		}
	}

};