|
|
|
@ -6,54 +6,29 @@
|
|
|
|
|
|
|
|
|
|
#define TAG_SPI "SPI"
|
|
|
|
|
|
|
|
|
|
const unsigned char EK_CMD[5] = { 0x80,0xd4,0x01,0x00,0x10 };
|
|
|
|
|
const unsigned char AK_CMD[5] = { 0x80,0xd4,0x02,0x00,0x10 };
|
|
|
|
|
const unsigned char IV_CMD[5] = { 0x80,0xd4,0x04,0x00,0x10 };
|
|
|
|
|
unsigned char SM1encrpt_CMD[5] = { 0xa0,0xe0,0x80,0xff,0xff };
|
|
|
|
|
unsigned char SM1decoder_CMD[5] = { 0xa0,0xe0,0x81,0xff,0xff };
|
|
|
|
|
unsigned char SM2Keypair_CMD[5] = { 0x80,0xb2,0x00,0xff,0x00 };
|
|
|
|
|
unsigned char SM2OutPub_CMD[5] = { 0x80,0xb8,0x01,0xff,0x40 };
|
|
|
|
|
unsigned char SM2OutPri_CMD[5] = { 0x80,0xb8,0x02,0xff,0x20 };
|
|
|
|
|
unsigned char SM2InPub_CMD[5] = { 0x80,0xba,0x01,0xff,0x40 };
|
|
|
|
|
unsigned char SM2InPri_CMD[5] = { 0x80,0xba,0x02,0xff,0x20 };
|
|
|
|
|
unsigned char SM3Hash_CMD[5] = { 0x80,0xb5,0x00,0xff,0xff };
|
|
|
|
|
unsigned char SM2Sign_CMD[5] = { 0x80,0xb4,0x00,0xff,0x20 };
|
|
|
|
|
unsigned char SM2VerifySign_CMD[5] = { 0x80,0xb6,0x00,0xff,0x60 };
|
|
|
|
|
unsigned char SM2encrypt_CMD[5] = { 0x80,0xb3,0x01,0xff,0x20 };
|
|
|
|
|
unsigned char SM2decoder_CMD[5] = { 0x80,0xb3,0x81,0xff,0x80 };
|
|
|
|
|
unsigned char SM2cert_CMD[5] = { 0x80,0xb7,0xff,0xff,0xff };
|
|
|
|
|
unsigned char Random_CMD[5] = { 0x00,0x84,0x00,0x00,0xff };
|
|
|
|
|
const unsigned char Version_CMD[5] = { 0x00,0x5b,0x00,0x00,0x40 };
|
|
|
|
|
const unsigned char Indentify_CMD[5] = { 0x80,0xb3,0x01,0x04,0x20 };
|
|
|
|
|
|
|
|
|
|
NrsecPort::NrsecPort()
|
|
|
|
|
const uint8_t EK_CMD[5] = { 0x80,0xd4,0x01,0x00,0x10 };
|
|
|
|
|
const uint8_t AK_CMD[5] = { 0x80,0xd4,0x02,0x00,0x10 };
|
|
|
|
|
const uint8_t IV_CMD[5] = { 0x80,0xd4,0x04,0x00,0x10 };
|
|
|
|
|
uint8_t SM1encrpt_CMD[5] = { 0xa0,0xe0,0x80,0xff,0xff };
|
|
|
|
|
uint8_t SM1decoder_CMD[5] = { 0xa0,0xe0,0x81,0xff,0xff };
|
|
|
|
|
uint8_t SM2Keypair_CMD[5] = { 0x80,0xb2,0x00,0xff,0x00 };
|
|
|
|
|
uint8_t SM2OutPub_CMD[5] = { 0x80,0xb8,0x01,0xff,0x40 };
|
|
|
|
|
uint8_t SM2OutPri_CMD[5] = { 0x80,0xb8,0x02,0xff,0x20 };
|
|
|
|
|
uint8_t SM2InPub_CMD[5] = { 0x80,0xba,0x01,0xff,0x40 };
|
|
|
|
|
uint8_t SM2InPri_CMD[5] = { 0x80,0xba,0x02,0xff,0x20 };
|
|
|
|
|
uint8_t SM3Hash_CMD[5] = { 0x80,0xb5,0x00,0xff,0xff };
|
|
|
|
|
uint8_t SM2Sign_CMD[5] = { 0x80,0xb4,0x00,0xff,0x20 };
|
|
|
|
|
uint8_t SM2VerifySign_CMD[5] = { 0x80,0xb6,0x00,0xff,0x60 };
|
|
|
|
|
uint8_t SM2encrypt_CMD[5] = { 0x80,0xb3,0x01,0xff,0x20 };
|
|
|
|
|
uint8_t SM2decoder_CMD[5] = { 0x80,0xb3,0x81,0xff,0x80 };
|
|
|
|
|
uint8_t SM2cert_CMD[5] = { 0x80,0xb7,0xff,0xff,0xff };
|
|
|
|
|
uint8_t Random_CMD[5] = { 0x00,0x84,0x00,0x00,0xff };
|
|
|
|
|
const uint8_t Version_CMD[5] = { 0x00,0x5b,0x00,0x00,0x40 };
|
|
|
|
|
const uint8_t Indentify_CMD[5] = { 0x80,0xb3,0x01,0x04,0x20 };
|
|
|
|
|
|
|
|
|
|
uint8_t NrsecPort::CalcCRC7(const uint8_t *buff, int len)
|
|
|
|
|
{
|
|
|
|
|
m_fd = 0;
|
|
|
|
|
}
|
|
|
|
|
bool NrsecPort::Open(const char *path)
|
|
|
|
|
{
|
|
|
|
|
m_path = path;
|
|
|
|
|
m_fd = open(path, O_RDWR);
|
|
|
|
|
if (m_fd < 0) {
|
|
|
|
|
perror("open");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
spi_master_init(path, m_fd);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NrsecPort::~NrsecPort()
|
|
|
|
|
{
|
|
|
|
|
if (m_fd > 0)
|
|
|
|
|
{
|
|
|
|
|
close(m_fd);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned char NrsecPort::get_crc7(const unsigned char *buff, int len)
|
|
|
|
|
{
|
|
|
|
|
const static unsigned char crc7_table[256] = {
|
|
|
|
|
const static uint8_t crc7_table[256] = {
|
|
|
|
|
0x00, 0x09, 0x12, 0x1b, 0x24, 0x2d, 0x36, 0x3f,
|
|
|
|
|
0x48, 0x41, 0x5a, 0x53, 0x6c, 0x65, 0x7e, 0x77,
|
|
|
|
|
0x19, 0x10, 0x0b, 0x02, 0x3d, 0x34, 0x2f, 0x26,
|
|
|
|
@ -88,7 +63,7 @@ unsigned char NrsecPort::get_crc7(const unsigned char *buff, int len)
|
|
|
|
|
0x46, 0x4f, 0x54, 0x5d, 0x62, 0x6b, 0x70, 0x79
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
unsigned char crc7_accum = 0;
|
|
|
|
|
uint8_t crc7_accum = 0;
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
@ -98,7 +73,7 @@ unsigned char NrsecPort::get_crc7(const unsigned char *buff, int len)
|
|
|
|
|
return crc7_accum;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NrsecPort::SendCMD(uint8_t*cmd, uint8_t*rxbuf)
|
|
|
|
|
void NrsecPort::SendCMD(uint8_t* cmd, uint8_t* rxbuf)
|
|
|
|
|
{
|
|
|
|
|
int i = 0;
|
|
|
|
|
int retval;
|
|
|
|
@ -115,7 +90,7 @@ void NrsecPort::SendCMD(uint8_t*cmd, uint8_t*rxbuf)
|
|
|
|
|
/* send five command header */
|
|
|
|
|
for (i = 0; i < CMD_HEAD_SIZE; i++)
|
|
|
|
|
{
|
|
|
|
|
retval = spi_transfer(m_fd, cmd + i, rxbuf + i, 1);
|
|
|
|
|
retval = spi_transfer(cmd + i, rxbuf + i, 1);
|
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, TAG_SPI, "cmd[%d]=%x,rxbuf[%d]=%x", i, *(cmd + i), i, *(rxbuf + i));
|
|
|
|
|
delay(20);
|
|
|
|
|
}
|
|
|
|
@ -134,7 +109,7 @@ INS:
|
|
|
|
|
delay(20);
|
|
|
|
|
while (cnt--)
|
|
|
|
|
{
|
|
|
|
|
retval = spi_transfer(m_fd, txbuf, buf, 1);
|
|
|
|
|
retval = spi_transfer(txbuf, buf, 1);
|
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, TAG_SPI, "RcvINS txbuf=%x,buf=%x", *txbuf, *buf);
|
|
|
|
|
if (*buf == ins)
|
|
|
|
|
{
|
|
|
|
@ -146,14 +121,14 @@ INS:
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NrsecPort::RcvLEN(uint8_t*txbuf, uint8_t*buf, uint8_t len)
|
|
|
|
|
void NrsecPort::RcvLEN(uint8_t* txbuf, uint8_t* buf, uint8_t len)
|
|
|
|
|
{
|
|
|
|
|
int retval;
|
|
|
|
|
/* receive length */
|
|
|
|
|
LEN:
|
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
|
txbuf[0] = 0xaa;
|
|
|
|
|
retval = spi_transfer(m_fd, txbuf, buf + i, 1);
|
|
|
|
|
retval = spi_transfer(txbuf, buf + i, 1);
|
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, TAG_SPI, "RecvLEN txbuf=%x,rxbuf=%x", *txbuf, *(buf + i));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -178,7 +153,7 @@ void NrsecPort::RcvData(uint8_t*txbuf, uint8_t*buf, int len)
|
|
|
|
|
/* receive data and crc */
|
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
|
{
|
|
|
|
|
retval = spi_transfer(m_fd, txbuf, buf + i, 1);
|
|
|
|
|
retval = spi_transfer(txbuf, buf + i, 1);
|
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, TAG_SPI, "RcvData data[%d]=%x", i, *(buf + i));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -196,7 +171,7 @@ SW90:
|
|
|
|
|
delay(20);
|
|
|
|
|
while (1)
|
|
|
|
|
{
|
|
|
|
|
retval = spi_transfer(m_fd, txbuf, buf, 1);
|
|
|
|
|
retval = spi_transfer(txbuf, buf, 1);
|
|
|
|
|
|
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, TAG_SPI, "RecvSW txbuf=%x,buf=%x", *txbuf, *buf);
|
|
|
|
|
|
|
|
|
@ -206,14 +181,14 @@ SW90:
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
retval = spi_transfer(m_fd, txbuf, buf + 1, 1);
|
|
|
|
|
retval = spi_transfer(txbuf, buf + 1, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NrsecPort::SendEnd(uint8_t* txbuf, uint8_t* buf)
|
|
|
|
|
{
|
|
|
|
|
int retval;
|
|
|
|
|
txbuf[0] = 0xaa;
|
|
|
|
|
retval = spi_transfer(m_fd, txbuf, buf, 1);
|
|
|
|
|
retval = spi_transfer(txbuf, buf, 1);
|
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, TAG_SPI, "SendEnd txbuf=%x,rxbuf=%hhu", *txbuf, *buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -221,7 +196,7 @@ void NrsecPort::SendId(uint8_t* txbuf, uint8_t* buf, uint8_t id)
|
|
|
|
|
{
|
|
|
|
|
int retval;
|
|
|
|
|
txbuf[0] = id;
|
|
|
|
|
retval = spi_transfer(m_fd, txbuf, buf, 1);
|
|
|
|
|
retval = spi_transfer(txbuf, buf, 1);
|
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, TAG_SPI, "SendID txbuf=%x,rxbuf=%hhu", *txbuf, *buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -229,16 +204,16 @@ void NrsecPort::SendData(uint8_t* data, uint8_t* rxbuf, int data_size)
|
|
|
|
|
{
|
|
|
|
|
int i = 0;
|
|
|
|
|
int retval;
|
|
|
|
|
unsigned char crc[1];
|
|
|
|
|
crc[0] = get_crc7(data, data_size);
|
|
|
|
|
uint8_t crc[1];
|
|
|
|
|
crc[0] = CalcCRC7(data, data_size);
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < data_size; i++)
|
|
|
|
|
{
|
|
|
|
|
retval = spi_transfer(m_fd, data + i, rxbuf + i, 1);
|
|
|
|
|
retval = spi_transfer(data + i, rxbuf + i, 1);
|
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, TAG_SPI, "SendData i=%d,txbuf=%x,rxbuf=%x", i, *(data + i), *(rxbuf + i));
|
|
|
|
|
delay(20);
|
|
|
|
|
}
|
|
|
|
|
retval = spi_transfer(m_fd, crc, rxbuf, 1);
|
|
|
|
|
retval = spi_transfer(crc, rxbuf, 1);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -246,8 +221,8 @@ int NrsecPort::Spirandom()
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
int cnt;
|
|
|
|
|
unsigned char txbuf[256];
|
|
|
|
|
unsigned char rxbuf[256];
|
|
|
|
|
uint8_t txbuf[256];
|
|
|
|
|
uint8_t rxbuf[256];
|
|
|
|
|
|
|
|
|
|
int retval;
|
|
|
|
|
int msglen;
|
|
|
|
@ -273,7 +248,7 @@ CMD_RESEND:
|
|
|
|
|
RcvSW(txbuf, rxbuf + 2 + rxbuf[1], 0x90);
|
|
|
|
|
|
|
|
|
|
//计算接收到数据的CRC
|
|
|
|
|
if (get_crc7(rxbuf + 2, rxbuf[1] - 1) != rxbuf[rxbuf[1] + 1])
|
|
|
|
|
if (CalcCRC7(rxbuf + 2, rxbuf[1] - 1) != rxbuf[rxbuf[1] + 1])
|
|
|
|
|
{
|
|
|
|
|
//CRC Error 命令重发,超过3次,结束
|
|
|
|
|
if (cnt < 3)
|
|
|
|
@ -308,8 +283,8 @@ std::string NrsecPort::Version()
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
int cnt;
|
|
|
|
|
unsigned char txbuf[256];
|
|
|
|
|
unsigned char rxbuf[256];
|
|
|
|
|
uint8_t txbuf[256];
|
|
|
|
|
uint8_t rxbuf[256];
|
|
|
|
|
|
|
|
|
|
int retval;
|
|
|
|
|
int msglen;
|
|
|
|
@ -339,7 +314,7 @@ CMD_RESEND:
|
|
|
|
|
RcvSW(txbuf, rxbuf + 2 + rxbuf[1], 0x90);
|
|
|
|
|
|
|
|
|
|
//计算接收到数据的CRC
|
|
|
|
|
if (get_crc7(rxbuf + 2, rxbuf[1] - 1) != rxbuf[rxbuf[1] + 1])
|
|
|
|
|
if (CalcCRC7(rxbuf + 2, rxbuf[1] - 1) != rxbuf[rxbuf[1] + 1])
|
|
|
|
|
{
|
|
|
|
|
//CRC Error 命令重发,超过3次,结束
|
|
|
|
|
if (cnt < 3)
|
|
|
|
@ -377,8 +352,8 @@ int NrsecPort::SM2keypair(int index)//产生密钥
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
int cnt;
|
|
|
|
|
unsigned char txbuf[256];
|
|
|
|
|
unsigned char rxbuf[256];
|
|
|
|
|
uint8_t txbuf[256];
|
|
|
|
|
uint8_t rxbuf[256];
|
|
|
|
|
|
|
|
|
|
int retval;
|
|
|
|
|
int msglen;
|
|
|
|
@ -405,12 +380,12 @@ CMD_RESEND:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int NrsecPort::SM2ExportPublicKey(int index, unsigned char result[])//导出公钥
|
|
|
|
|
int NrsecPort::SM2ExportPublicKey(int index, uint8_t result[])//导出公钥
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
int cnt;
|
|
|
|
|
unsigned char txbuf[256];
|
|
|
|
|
unsigned char rxbuf[256];
|
|
|
|
|
uint8_t txbuf[256];
|
|
|
|
|
uint8_t rxbuf[256];
|
|
|
|
|
|
|
|
|
|
int retval;
|
|
|
|
|
int msglen;
|
|
|
|
@ -437,7 +412,7 @@ CMD_RESEND:
|
|
|
|
|
RcvSW(txbuf, rxbuf + 2 + rxbuf[1], 0x90);
|
|
|
|
|
|
|
|
|
|
//计算接收到数据的CRC
|
|
|
|
|
if (get_crc7(rxbuf + 2, rxbuf[1] - 1) != rxbuf[rxbuf[1] + 1])
|
|
|
|
|
if (CalcCRC7(rxbuf + 2, rxbuf[1] - 1) != rxbuf[rxbuf[1] + 1])
|
|
|
|
|
{
|
|
|
|
|
//CRC Error 命令重发,超过3次,结束
|
|
|
|
|
if (cnt < 3)
|
|
|
|
@ -464,12 +439,12 @@ CMD_RESEND:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int NrsecPort::SM2ExportPrivateKey(int index, unsigned char result[])//导出私钥
|
|
|
|
|
int NrsecPort::SM2ExportPrivateKey(int index, uint8_t result[])//导出私钥
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
int cnt;
|
|
|
|
|
unsigned char txbuf[256];
|
|
|
|
|
unsigned char rxbuf[256];
|
|
|
|
|
uint8_t txbuf[256];
|
|
|
|
|
uint8_t rxbuf[256];
|
|
|
|
|
|
|
|
|
|
int retval;
|
|
|
|
|
int msglen;
|
|
|
|
@ -498,7 +473,7 @@ CMD_RESEND:
|
|
|
|
|
RcvSW(txbuf, rxbuf + 2 + rxbuf[1], 0x90);
|
|
|
|
|
|
|
|
|
|
//计算接收到数据的CRC
|
|
|
|
|
if (get_crc7(rxbuf + 2, rxbuf[1] - 1) != rxbuf[rxbuf[1] + 1])
|
|
|
|
|
if (CalcCRC7(rxbuf + 2, rxbuf[1] - 1) != rxbuf[rxbuf[1] + 1])
|
|
|
|
|
{
|
|
|
|
|
//CRC Error 命令重发,超过3次,结束
|
|
|
|
|
if (cnt < 3)
|
|
|
|
@ -534,12 +509,12 @@ CMD_RESEND:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int NrsecPort::SM2InportPublicKey(int index, const unsigned char new_key[])//外部公钥导入存放在01
|
|
|
|
|
int NrsecPort::SM2InportPublicKey(int index, const uint8_t new_key[])//外部公钥导入存放在01
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
int cnt;
|
|
|
|
|
unsigned char txbuf[256];
|
|
|
|
|
unsigned char rxbuf[256];
|
|
|
|
|
uint8_t txbuf[256];
|
|
|
|
|
uint8_t rxbuf[256];
|
|
|
|
|
|
|
|
|
|
int retval;
|
|
|
|
|
int msglen;
|
|
|
|
@ -580,12 +555,12 @@ CMD_RESEND:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int NrsecPort::SM2InportPrivateKey(int index, const unsigned char new_key[])//导入私钥 没测试
|
|
|
|
|
int NrsecPort::SM2InportPrivateKey(int index, const uint8_t new_key[])//导入私钥 没测试
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
int cnt;
|
|
|
|
|
unsigned char txbuf[256];
|
|
|
|
|
unsigned char rxbuf[256];
|
|
|
|
|
uint8_t txbuf[256];
|
|
|
|
|
uint8_t rxbuf[256];
|
|
|
|
|
|
|
|
|
|
int retval;
|
|
|
|
|
int msglen;
|
|
|
|
@ -624,12 +599,12 @@ CMD_RESEND:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int NrsecPort::SM3Hash(unsigned char *to_hash, int len, unsigned char *out_hash)//原始哈希 跑通了,没有验证
|
|
|
|
|
int NrsecPort::SM3Hash(uint8_t *to_hash, int len, uint8_t *out_hash)//原始哈希 跑通了,没有验证
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
int cnt;
|
|
|
|
|
unsigned char txbuf[512];
|
|
|
|
|
unsigned char rxbuf[512];
|
|
|
|
|
uint8_t txbuf[512];
|
|
|
|
|
uint8_t rxbuf[512];
|
|
|
|
|
|
|
|
|
|
int retval;
|
|
|
|
|
int msglen;
|
|
|
|
@ -688,13 +663,13 @@ CMD_RESEND:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int NrsecPort::sm3hash_tosm2(unsigned char *in, int inl, unsigned char *out, unsigned char *pubkey, unsigned char* pucID, int idl)
|
|
|
|
|
int NrsecPort::sm3hash_tosm2(uint8_t *in, int inl, uint8_t *out, uint8_t *pubkey, uint8_t* pucID, int idl)
|
|
|
|
|
{
|
|
|
|
|
int nRet, l;
|
|
|
|
|
unsigned char *Z = NULL;
|
|
|
|
|
uint8_t *Z = NULL;
|
|
|
|
|
int entl = 0;
|
|
|
|
|
unsigned char tmpm[32];
|
|
|
|
|
unsigned char abxy[32 * 4] = {
|
|
|
|
|
uint8_t tmpm[32];
|
|
|
|
|
uint8_t abxy[32 * 4] = {
|
|
|
|
|
0xFF,0xFF,0xFF,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, /* a */
|
|
|
|
|
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
|
|
|
|
|
0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
|
|
|
|
@ -713,7 +688,7 @@ int NrsecPort::sm3hash_tosm2(unsigned char *in, int inl, unsigned char *out, uns
|
|
|
|
|
0xF0,0xA0
|
|
|
|
|
};
|
|
|
|
|
l = 2 + idl + 32 * 6;
|
|
|
|
|
Z = (unsigned char *)malloc(l);
|
|
|
|
|
Z = (uint8_t *)malloc(l);
|
|
|
|
|
if (!Z)
|
|
|
|
|
return -1;
|
|
|
|
|
entl = idl * 8;
|
|
|
|
@ -729,7 +704,7 @@ int NrsecPort::sm3hash_tosm2(unsigned char *in, int inl, unsigned char *out, uns
|
|
|
|
|
goto quit;
|
|
|
|
|
free(Z);
|
|
|
|
|
l = inl + 32;
|
|
|
|
|
Z = (unsigned char *)malloc(l);
|
|
|
|
|
Z = (uint8_t *)malloc(l);
|
|
|
|
|
if (!Z) {
|
|
|
|
|
nRet = -1;
|
|
|
|
|
goto quit;
|
|
|
|
@ -743,12 +718,12 @@ quit:
|
|
|
|
|
return nRet;
|
|
|
|
|
}//用于签名的处理哈希值,用户标识01*16
|
|
|
|
|
|
|
|
|
|
int NrsecPort::SM2Sign(int index, const unsigned char *to_sign, unsigned char *out_sign)//SM2签名 所使用的哈希值应该来源于sm3hash_tosm2
|
|
|
|
|
int NrsecPort::SM2Sign(int index, const uint8_t *to_sign, uint8_t *out_sign)//SM2签名 所使用的哈希值应该来源于sm3hash_tosm2
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
int cnt;
|
|
|
|
|
unsigned char txbuf[256];
|
|
|
|
|
unsigned char rxbuf[256];
|
|
|
|
|
uint8_t txbuf[256];
|
|
|
|
|
uint8_t rxbuf[256];
|
|
|
|
|
|
|
|
|
|
int retval;
|
|
|
|
|
int msglen;
|
|
|
|
@ -796,10 +771,10 @@ CMD_RESEND:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int NrsecPort::SM2VerifySign(int index, unsigned char *hash, unsigned char *vs)//SM2验签
|
|
|
|
|
int NrsecPort::SM2VerifySign(int index, uint8_t *hash, uint8_t *vs)//SM2验签
|
|
|
|
|
{
|
|
|
|
|
unsigned char txbuf[256];
|
|
|
|
|
unsigned char rxbuf[256];
|
|
|
|
|
uint8_t txbuf[256];
|
|
|
|
|
uint8_t rxbuf[256];
|
|
|
|
|
|
|
|
|
|
int retval;
|
|
|
|
|
int msglen;
|
|
|
|
@ -836,12 +811,12 @@ CMD_RESEND:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int NrsecPort::SM2encrypt(int index, unsigned char *to_encrypt, unsigned char *out_encrypt)//加密
|
|
|
|
|
int NrsecPort::SM2encrypt(int index, uint8_t *to_encrypt, uint8_t *out_encrypt)//加密
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
int cnt;
|
|
|
|
|
unsigned char txbuf[512];
|
|
|
|
|
unsigned char rxbuf[512];
|
|
|
|
|
uint8_t txbuf[512];
|
|
|
|
|
uint8_t rxbuf[512];
|
|
|
|
|
|
|
|
|
|
int retval;
|
|
|
|
|
int msglen;
|
|
|
|
@ -889,12 +864,12 @@ CMD_RESEND:
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
int NrsecPort::SM2decoder(int index, unsigned char *to_decoder, unsigned char *out_decoder)//解密
|
|
|
|
|
int NrsecPort::SM2decoder(int index, uint8_t *to_decoder, uint8_t *out_decoder)//解密
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
int cnt;
|
|
|
|
|
unsigned char txbuf[512];
|
|
|
|
|
unsigned char rxbuf[512];
|
|
|
|
|
uint8_t txbuf[512];
|
|
|
|
|
uint8_t rxbuf[512];
|
|
|
|
|
|
|
|
|
|
int retval;
|
|
|
|
|
int msglen;
|
|
|
|
@ -943,12 +918,12 @@ CMD_RESEND:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int NrsecPort::SM2cert(int type, int index, string cert, unsigned char *out_cert)//证书
|
|
|
|
|
int NrsecPort::SM2cert(int type, int index, string cert, uint8_t *out_cert)//证书
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
int cnt;
|
|
|
|
|
unsigned char txbuf[512];
|
|
|
|
|
unsigned char rxbuf[1024];
|
|
|
|
|
uint8_t txbuf[512];
|
|
|
|
|
uint8_t rxbuf[1024];
|
|
|
|
|
|
|
|
|
|
int retval;
|
|
|
|
|
int msglen;
|
|
|
|
@ -960,7 +935,7 @@ int NrsecPort::SM2cert(int type, int index, string cert, unsigned char *out_cert
|
|
|
|
|
//printf("tx %1d bytes: ", msglen);
|
|
|
|
|
|
|
|
|
|
int certlen = cert.length();
|
|
|
|
|
unsigned char to_cert[certlen];
|
|
|
|
|
uint8_t to_cert[certlen];
|
|
|
|
|
for (int x = 0; x < certlen; x++) {
|
|
|
|
|
to_cert[x] = cert[x];
|
|
|
|
|
}
|
|
|
|
@ -1015,12 +990,12 @@ CMD_RESEND:
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
int NrsecPort::Indentify(unsigned char *to_idt, unsigned char *out_idt)//安全认证
|
|
|
|
|
int NrsecPort::Indentify(uint8_t *to_idt, uint8_t *out_idt)//安全认证
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
int cnt;
|
|
|
|
|
unsigned char txbuf[512];
|
|
|
|
|
unsigned char rxbuf[512];
|
|
|
|
|
uint8_t txbuf[512];
|
|
|
|
|
uint8_t rxbuf[512];
|
|
|
|
|
|
|
|
|
|
int retval;
|
|
|
|
|
int msglen;
|
|
|
|
|