PBOCcmd.cpp
6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* 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;
}
}
};