|
|
|
@ -228,6 +228,58 @@ void NrsecPort::SendData(uint8_t* data, uint8_t* rxbuf, int data_size)
|
|
|
|
|
retval = spi_transfer(crc, rxbuf, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int NrsecPort::SM1ImportKey(const uint8_t* ek, const uint8_t* ak)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
int cnt;
|
|
|
|
|
uint8_t txbuf[256];
|
|
|
|
|
uint8_t rxbuf[256];
|
|
|
|
|
|
|
|
|
|
int retval;
|
|
|
|
|
int msglen;
|
|
|
|
|
|
|
|
|
|
cnt = 0;
|
|
|
|
|
msglen = 5;
|
|
|
|
|
memset(rxbuf, 0, sizeof(rxbuf));
|
|
|
|
|
memset(txbuf, 0, sizeof(txbuf));
|
|
|
|
|
|
|
|
|
|
CMD_RESEND:
|
|
|
|
|
|
|
|
|
|
memcpy(txbuf, (const void *)EK_CMD, sizeof(EK_CMD));
|
|
|
|
|
|
|
|
|
|
SendCMD(txbuf, rxbuf);
|
|
|
|
|
|
|
|
|
|
RcvINS(txbuf, rxbuf, txbuf[1]); // 指令
|
|
|
|
|
|
|
|
|
|
SendId(txbuf, rxbuf, 0x55);
|
|
|
|
|
|
|
|
|
|
memcpy(txbuf, ek, 16);
|
|
|
|
|
// txbuf[16] = CalcCRC7(ek, 16);
|
|
|
|
|
|
|
|
|
|
SendData(txbuf, rxbuf, 16);
|
|
|
|
|
|
|
|
|
|
RcvSW(txbuf, rxbuf, 0x90);
|
|
|
|
|
|
|
|
|
|
memcpy(txbuf, (const void *)AK_CMD, sizeof(AK_CMD));
|
|
|
|
|
|
|
|
|
|
SendCMD(txbuf, rxbuf);
|
|
|
|
|
|
|
|
|
|
RcvINS(txbuf, rxbuf, txbuf[1]); // 指令
|
|
|
|
|
|
|
|
|
|
SendId(txbuf, rxbuf, 0x55);
|
|
|
|
|
|
|
|
|
|
memcpy(txbuf, ak, 16);
|
|
|
|
|
// txbuf[16] = CalcCRC7(ak, 16);
|
|
|
|
|
|
|
|
|
|
SendData(txbuf, rxbuf, 16);//senddata函数中已经包含crc了
|
|
|
|
|
|
|
|
|
|
SendEnd(txbuf, rxbuf);
|
|
|
|
|
|
|
|
|
|
RcvSW(txbuf, rxbuf, 0x90);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int NrsecPort::ImportIV(const uint8_t* iv, uint8_t ivLength)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
@ -254,9 +306,9 @@ CMD_RESEND:
|
|
|
|
|
SendId(txbuf, rxbuf, 0x55);
|
|
|
|
|
|
|
|
|
|
memcpy(txbuf, iv, ivLength);
|
|
|
|
|
txbuf[ivLength] = CalcCRC7(iv, ivLength);
|
|
|
|
|
// txbuf[ivLength] = CalcCRC7(iv, ivLength);
|
|
|
|
|
|
|
|
|
|
SendData(txbuf, rxbuf, ivLength + 1);
|
|
|
|
|
SendData(txbuf, rxbuf, ivLength);
|
|
|
|
|
|
|
|
|
|
SendEnd(txbuf, rxbuf);
|
|
|
|
|
|
|
|
|
@ -295,16 +347,25 @@ int NrsecPort::SM1Encrypt(const uint8_t* data, uint16_t dataLen, uint8_t* encryp
|
|
|
|
|
|
|
|
|
|
SendId(txbuf, rxbuf, 0x55);
|
|
|
|
|
|
|
|
|
|
RcvLEN(txbuf, rxbuf + 1, 1); //长度 多加一个字节的 CRC
|
|
|
|
|
memcpy(txbuf,data,dataLen);
|
|
|
|
|
|
|
|
|
|
uint8_t len = *(rxbuf + 1);
|
|
|
|
|
SendData(txbuf,rxbuf,dataLen);
|
|
|
|
|
|
|
|
|
|
RcvData(txbuf, rxbuf + 2, len);
|
|
|
|
|
SendEnd(txbuf,rxbuf);
|
|
|
|
|
|
|
|
|
|
RcvSW(txbuf, rxbuf + 2 + len, 0x90);
|
|
|
|
|
RcvINS(txbuf,rxbuf,0xe0);
|
|
|
|
|
|
|
|
|
|
RcvLEN(txbuf, rxbuf + 1, 2); //长度 多加一个字节的 CRC
|
|
|
|
|
|
|
|
|
|
// uint8_t len = *(rxbuf + 1);
|
|
|
|
|
uint8_t len =(*(rxbuf+1)<<8)+(*(rxbuf+2));
|
|
|
|
|
|
|
|
|
|
RcvData(txbuf, rxbuf + 3, len);
|
|
|
|
|
|
|
|
|
|
RcvSW(txbuf, rxbuf + 3 + len, 0x90);
|
|
|
|
|
|
|
|
|
|
//计算接收到数据的CRC
|
|
|
|
|
if (CalcCRC7(rxbuf + 2, rxbuf[1] - 1) != rxbuf[rxbuf[1] + 1])
|
|
|
|
|
if (CalcCRC7(rxbuf + 3, len-1) != rxbuf[len + 1])
|
|
|
|
|
{
|
|
|
|
|
//CRC Error 命令重发,超过3次,结束
|
|
|
|
|
if (cnt < 3)
|
|
|
|
@ -324,9 +385,83 @@ int NrsecPort::SM1Encrypt(const uint8_t* data, uint16_t dataLen, uint8_t* encryp
|
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, TAG_SPI, "rx %1d bytes:", rxbuf[1] + 4);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
memcpy(output, rxbuf + 2, len);
|
|
|
|
|
memcpy(encryptedData, rxbuf + 3, len-1);
|
|
|
|
|
memcpy(&bufferLen, &len-1, 1);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int NrsecPort::SM1Decrypt(const uint8_t* encryptedData, uint16_t encryptedDataLen, uint8_t* data, uint16_t bufferLen)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
int cnt;
|
|
|
|
|
uint8_t txbuf[256];
|
|
|
|
|
uint8_t rxbuf[256];
|
|
|
|
|
|
|
|
|
|
int retval;
|
|
|
|
|
int msglen;
|
|
|
|
|
|
|
|
|
|
cnt = 0;
|
|
|
|
|
msglen = 5;
|
|
|
|
|
memset(rxbuf, 0, sizeof(rxbuf));
|
|
|
|
|
memset(txbuf, 0, sizeof(txbuf));
|
|
|
|
|
|
|
|
|
|
//printf("tx %1d bytes: ", msglen);
|
|
|
|
|
|
|
|
|
|
CMD_RESEND:
|
|
|
|
|
|
|
|
|
|
memcpy(txbuf, (const void *)SM1decoder_CMD, sizeof(SM1decoder_CMD));
|
|
|
|
|
|
|
|
|
|
txbuf[3] = encryptedDataLen >> 8;
|
|
|
|
|
txbuf[4] = encryptedDataLen & 0xFF;
|
|
|
|
|
|
|
|
|
|
SendCMD(txbuf, rxbuf);
|
|
|
|
|
|
|
|
|
|
RcvINS(txbuf, rxbuf, txbuf[1]); // 指令
|
|
|
|
|
|
|
|
|
|
SendId(txbuf, rxbuf, 0x55);
|
|
|
|
|
|
|
|
|
|
memcpy(txbuf,encryptedData,encryptedDataLen);
|
|
|
|
|
|
|
|
|
|
SendData(txbuf,rxbuf,encryptedDataLen);
|
|
|
|
|
|
|
|
|
|
SendEnd(txbuf,rxbuf);
|
|
|
|
|
|
|
|
|
|
RcvINS(txbuf,rxbuf,0xe0);
|
|
|
|
|
|
|
|
|
|
RcvLEN(txbuf, rxbuf + 1, 2); //长度 多加一个字节的 CRC
|
|
|
|
|
|
|
|
|
|
// uint8_t len = *(rxbuf + 1);
|
|
|
|
|
uint8_t len =(*(rxbuf+1)<<8)+(*(rxbuf+2));
|
|
|
|
|
|
|
|
|
|
RcvData(txbuf, rxbuf + 3, len);
|
|
|
|
|
|
|
|
|
|
RcvSW(txbuf, rxbuf + 3 + len, 0x90);
|
|
|
|
|
|
|
|
|
|
//计算接收到数据的CRC
|
|
|
|
|
if (CalcCRC7(rxbuf + 3, len-1) != rxbuf[len + 1])
|
|
|
|
|
{
|
|
|
|
|
//CRC Error 命令重发,超过3次,结束
|
|
|
|
|
if (cnt < 3)
|
|
|
|
|
{
|
|
|
|
|
cnt++;
|
|
|
|
|
goto CMD_RESEND;
|
|
|
|
|
printf("cnt over\n");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
printf("ERROR\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// printf("rx %1d bytes: ", rxbuf[1] + 4);
|
|
|
|
|
#ifdef __ANDROID__
|
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, TAG_SPI, "rx %1d bytes:", rxbuf[1] + 4);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
memcpy(data, rxbuf + 3, len-1);
|
|
|
|
|
memcpy(&bufferLen, &len-1, 1);
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int NrsecPort::Random(uint8_t* output, uint8_t length)
|
|
|
|
@ -385,7 +520,7 @@ CMD_RESEND:
|
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, TAG_SPI, "rx %1d bytes:", rxbuf[1] + 4);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
memcpy(output, rxbuf + 2, len);
|
|
|
|
|
memcpy(output, rxbuf + 2, len-1);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
@ -447,7 +582,7 @@ CMD_RESEND:
|
|
|
|
|
#endif
|
|
|
|
|
std::string version = "";
|
|
|
|
|
char output[16] = { 0 };
|
|
|
|
|
for (i = 0; i < dataLen; i++) {
|
|
|
|
|
for (i = 0; i < dataLen-1; i++) {
|
|
|
|
|
if (*(rxbuf + 2 + i) == 0) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
@ -548,13 +683,7 @@ CMD_RESEND:
|
|
|
|
|
#ifdef __ANDROID__
|
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, TAG_SPI, "rx %1d bytes:", rxbuf[1] + 4);
|
|
|
|
|
#endif
|
|
|
|
|
//std::string result = "SM2公钥: ";
|
|
|
|
|
//char output[16] = { 0 };
|
|
|
|
|
for (i = 2; i < rxbuf[1] + 2; i++) {
|
|
|
|
|
//sprintf(output, " %02x ", rxbuf[i]);
|
|
|
|
|
result[i - 2] = rxbuf[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memcpy(result, rxbuf + 2, rxbuf[1]-1);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -612,20 +741,8 @@ CMD_RESEND:
|
|
|
|
|
#ifdef __ANDROID__
|
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, TAG_SPI, "rx %1d bytes:", rxbuf[1] + 4);
|
|
|
|
|
#endif
|
|
|
|
|
for (i = 2; i < rxbuf[1] + 2; i++) {
|
|
|
|
|
//sprintf(output, " %02x ", rxbuf[i]);
|
|
|
|
|
result[i - 2] = rxbuf[i];
|
|
|
|
|
}
|
|
|
|
|
// std::string result = "OutPri";
|
|
|
|
|
// char output[16] = { 0 };
|
|
|
|
|
// for (i = 0; i < rxbuf[1]+4; i++) {
|
|
|
|
|
// sprintf(output, " %c ", rxbuf[i]);
|
|
|
|
|
// result += output;
|
|
|
|
|
// }
|
|
|
|
|
// __android_log_print(ANDROID_LOG_INFO, "SPi", "%s", result.c_str());
|
|
|
|
|
// //__android_log_print(ANDROID_LOG_INFO, "SPi", "%s", rxbuf);
|
|
|
|
|
// // printf("\n");
|
|
|
|
|
|
|
|
|
|
memcpy(result, rxbuf + 2, rxbuf[1]-1);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
@ -738,16 +855,10 @@ int NrsecPort::SM3Hash(uint8_t *to_hash, int len, uint8_t *out_hash)//原始哈
|
|
|
|
|
memset(rxbuf, 0, sizeof(rxbuf));
|
|
|
|
|
memset(txbuf, 0, sizeof(txbuf));
|
|
|
|
|
|
|
|
|
|
int re[4] = { 0 }; // 余数数组
|
|
|
|
|
int x = 0; // 余数数组下标
|
|
|
|
|
int a = len;
|
|
|
|
|
while (a != 0) {
|
|
|
|
|
re[x] = a % 16;
|
|
|
|
|
a /= 16;
|
|
|
|
|
x++;
|
|
|
|
|
}
|
|
|
|
|
SM3Hash_CMD[3] = re[2] + re[3] * 16;
|
|
|
|
|
SM3Hash_CMD[4] = re[0] + re[1] * 16;
|
|
|
|
|
|
|
|
|
|
SM3Hash_CMD[3] = len >> 8;
|
|
|
|
|
SM3Hash_CMD[4] = len & 0xFF;
|
|
|
|
|
|
|
|
|
|
#ifdef __ANDROID__
|
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, "len", "len=%x", len);
|
|
|
|
|
#endif
|
|
|
|
@ -781,11 +892,8 @@ CMD_RESEND:
|
|
|
|
|
RcvSW(txbuf, rxbuf + 2 + rxbuf[1], 0x90);
|
|
|
|
|
|
|
|
|
|
//__android_log_print(ANDROID_LOG_INFO, "SPi", "rx %1d bytes:", rxbuf[1]+4);
|
|
|
|
|
for (i = 2; i < rxbuf[1] + 2; i++) {
|
|
|
|
|
//sprintf(output, " %02x ", rxbuf[i]);
|
|
|
|
|
out_hash[i - 2] = rxbuf[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memcpy(out_hash, rxbuf + 2, rxbuf[1]-1);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
@ -891,11 +999,7 @@ CMD_RESEND:
|
|
|
|
|
#ifdef __ANDROID__
|
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, TAG_SPI, "rx %1d bytes:", rxbuf[1] + 4);
|
|
|
|
|
#endif
|
|
|
|
|
for (i = 2; i < rxbuf[1] + 2; i++) {
|
|
|
|
|
//sprintf(output, " %02x ", rxbuf[i]);
|
|
|
|
|
out_sign[i - 2] = rxbuf[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memcpy(out_sign, rxbuf + 2, rxbuf[1]-1);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -985,11 +1089,7 @@ CMD_RESEND:
|
|
|
|
|
RcvSW(txbuf, rxbuf + 2 + rxbuf[1], 0x90);
|
|
|
|
|
|
|
|
|
|
//__android_log_print(ANDROID_LOG_INFO, "SPi", "rx %1d bytes:", rxbuf[1]+4);
|
|
|
|
|
for (i = 2; i < rxbuf[1] + 2; i++) {
|
|
|
|
|
//sprintf(output, " %02x ", rxbuf[i]);
|
|
|
|
|
out_encrypt[i - 2] = rxbuf[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memcpy(out_encrypt, rxbuf + 2, rxbuf[1]-1);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
@ -1038,11 +1138,7 @@ CMD_RESEND:
|
|
|
|
|
RcvSW(txbuf, rxbuf + 2 + rxbuf[1], 0x90);
|
|
|
|
|
|
|
|
|
|
//__android_log_print(ANDROID_LOG_INFO, "SPi", "rx %1d bytes:", rxbuf[1]+4);
|
|
|
|
|
for (i = 2; i < rxbuf[1] + 2; i++) {
|
|
|
|
|
//sprintf(output, " %02x ", rxbuf[i]);
|
|
|
|
|
out_decoder[i - 2] = rxbuf[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memcpy(out_decoder, rxbuf + 2, rxbuf[1]-1);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
@ -1091,28 +1187,15 @@ CMD_RESEND:
|
|
|
|
|
|
|
|
|
|
RcvLEN(txbuf, rxbuf + 1, 2);
|
|
|
|
|
|
|
|
|
|
int re[4] = { 0 };
|
|
|
|
|
int x = 0;
|
|
|
|
|
int a = rxbuf[1];
|
|
|
|
|
while (a != 0) {
|
|
|
|
|
re[x] = a % 16;
|
|
|
|
|
a /= 16;
|
|
|
|
|
x++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int outlen = re[0] * 256 + re[1] * 4096 + rxbuf[2];
|
|
|
|
|
uint8_t outlen =(*(rxbuf+1)<<8)+(*(rxbuf+2));
|
|
|
|
|
|
|
|
|
|
RcvData(txbuf, rxbuf + 2, outlen);
|
|
|
|
|
RcvData(txbuf, rxbuf + 3, outlen);
|
|
|
|
|
|
|
|
|
|
RcvSW(txbuf, rxbuf + 2 + rxbuf[1], 0x90);
|
|
|
|
|
RcvSW(txbuf, rxbuf + 3 + rxbuf[1], 0x90);
|
|
|
|
|
|
|
|
|
|
//__android_log_print(ANDROID_LOG_INFO, "SPi", "rx %1d bytes:", rxbuf[1]+4);
|
|
|
|
|
|
|
|
|
|
for (i = 2; i < outlen + 2; i++) {
|
|
|
|
|
//sprintf(output, " %02x ", rxbuf[i]);
|
|
|
|
|
out_cert[i - 2] = rxbuf[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memcpy(out_cert, rxbuf + 3, outlen-1);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
@ -1160,11 +1243,7 @@ CMD_RESEND:
|
|
|
|
|
RcvSW(txbuf, rxbuf + 2 + rxbuf[1], 0x90);
|
|
|
|
|
|
|
|
|
|
//__android_log_print(ANDROID_LOG_INFO, "SPi", "rx %1d bytes:", rxbuf[1]+4);
|
|
|
|
|
for (i = 2; i < rxbuf[1] + 2; i++) {
|
|
|
|
|
//sprintf(output, " %02x ", rxbuf[i]);
|
|
|
|
|
out_idt[i - 2] = rxbuf[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memcpy(out_idt, rxbuf + 2, rxbuf[1]-1);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|