main
Matthew 1 year ago
parent 2db9e020d7
commit a410df4ae3

@ -1,5 +1,7 @@
#include "NrsecPort.h"
#ifdef __ANDROID__
#include <android/log.h>
#endif
#define RE_SUC 0x01
#define RE_ERROR 0x00
@ -9,7 +11,7 @@
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 SM1Encrypt_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 };
@ -91,7 +93,9 @@ void NrsecPort::SendCMD(uint8_t* cmd, uint8_t* rxbuf)
for (i = 0; i < CMD_HEAD_SIZE; i++)
{
retval = spi_transfer(cmd + i, rxbuf + i, 1);
#ifdef __ANDROID__
__android_log_print(ANDROID_LOG_INFO, TAG_SPI, "cmd[%d]=%x,rxbuf[%d]=%x", i, *(cmd + i), i, *(rxbuf + i));
#endif
delay(20);
}
@ -110,7 +114,9 @@ INS:
while (cnt--)
{
retval = spi_transfer(txbuf, buf, 1);
#ifdef __ANDROID__
__android_log_print(ANDROID_LOG_INFO, TAG_SPI, "RcvINS txbuf=%x,buf=%x", *txbuf, *buf);
#endif
if (*buf == ins)
{
return;
@ -129,7 +135,9 @@ LEN:
for (int i = 0; i < len; i++) {
txbuf[0] = 0xaa;
retval = spi_transfer(txbuf, buf + i, 1);
#ifdef __ANDROID__
__android_log_print(ANDROID_LOG_INFO, TAG_SPI, "RecvLEN txbuf=%x,rxbuf=%x", *txbuf, *(buf + i));
#endif
}
}
@ -158,8 +166,6 @@ void NrsecPort::RcvData(uint8_t*txbuf, uint8_t*buf, int len)
}
}
//RcvSW
void NrsecPort::RcvSW(uint8_t*txbuf, uint8_t*buf, uint8_t sw)
{
@ -172,9 +178,9 @@ SW90:
while (1)
{
retval = spi_transfer(txbuf, buf, 1);
#ifdef __ANDROID__
__android_log_print(ANDROID_LOG_INFO, TAG_SPI, "RecvSW txbuf=%x,buf=%x", *txbuf, *buf);
#endif
if (*buf != sw)
{
goto SW90;
@ -189,7 +195,9 @@ void NrsecPort::SendEnd(uint8_t* txbuf, uint8_t* buf)
int retval;
txbuf[0] = 0xaa;
retval = spi_transfer(txbuf, buf, 1);
#ifdef __ANDROID__
__android_log_print(ANDROID_LOG_INFO, TAG_SPI, "SendEnd txbuf=%x,rxbuf=%hhu", *txbuf, *buf);
#endif
}
void NrsecPort::SendId(uint8_t* txbuf, uint8_t* buf, uint8_t id)
@ -197,7 +205,9 @@ void NrsecPort::SendId(uint8_t* txbuf, uint8_t* buf, uint8_t id)
int retval;
txbuf[0] = id;
retval = spi_transfer(txbuf, buf, 1);
#ifdef __ANDROID__
__android_log_print(ANDROID_LOG_INFO, TAG_SPI, "SendID txbuf=%x,rxbuf=%hhu", *txbuf, *buf);
#endif
}
void NrsecPort::SendData(uint8_t* data, uint8_t* rxbuf, int data_size)
@ -210,14 +220,116 @@ void NrsecPort::SendData(uint8_t* data, uint8_t* rxbuf, int data_size)
for (i = 0; i < data_size; i++)
{
retval = spi_transfer(data + i, rxbuf + i, 1);
#ifdef __ANDROID__
__android_log_print(ANDROID_LOG_INFO, TAG_SPI, "SendData i=%d,txbuf=%x,rxbuf=%x", i, *(data + i), *(rxbuf + i));
#endif
delay(20);
}
retval = spi_transfer(crc, rxbuf, 1);
}
int NrsecPort::ImportIV(const uint8_t* iv, uint8_t ivLength)
{
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 *)IV_CMD, sizeof(IV_CMD));
SendCMD(txbuf, rxbuf);
RcvINS(txbuf, rxbuf, txbuf[1]); // 指令
SendId(txbuf, rxbuf, 0x55);
memcpy(txbuf, iv, ivLength);
txbuf[ivLength] = CalcCRC7(iv, ivLength);
SendData(txbuf, rxbuf, ivLength + 1);
SendEnd(txbuf, rxbuf);
RcvSW(txbuf, rxbuf, 0x90);
return 0;
}
int NrsecPort::SM1Encrypt(const uint8_t* data, uint16_t dataLen, uint8_t* encryptedData, 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 *)SM1Encrypt_CMD, sizeof(SM1Encrypt_CMD));
txbuf[3] = dataLen >> 8;
txbuf[4] = dataLen & 0xFF;
SendCMD(txbuf, rxbuf);
RcvINS(txbuf, rxbuf, txbuf[1]); // 指令
SendId(txbuf, rxbuf, 0x55);
RcvLEN(txbuf, rxbuf + 1, 1); //长度 多加一个字节的 CRC
uint8_t len = *(rxbuf + 1);
RcvData(txbuf, rxbuf + 2, len);
RcvSW(txbuf, rxbuf + 2 + len, 0x90);
//计算接收到数据的CRC
if (CalcCRC7(rxbuf + 2, rxbuf[1] - 1) != rxbuf[rxbuf[1] + 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(output, rxbuf + 2, len);
return 0;
}
int NrsecPort::Spirandom()
int NrsecPort::Random(uint8_t* output, uint8_t length)
{
int i;
int cnt;
@ -227,6 +339,7 @@ int NrsecPort::Spirandom()
int retval;
int msglen;
cnt = 0;
msglen = 5;
memset(rxbuf, 0, sizeof(rxbuf));
memset(txbuf, 0, sizeof(txbuf));
@ -237,15 +350,19 @@ CMD_RESEND:
memcpy(txbuf, (const void *)Random_CMD, sizeof(Random_CMD));
txbuf[4] = length;
SendCMD(txbuf, rxbuf);
RcvINS(txbuf, rxbuf, txbuf[1]); // 指令
RcvLEN(txbuf, rxbuf + 1, txbuf[4] + 1); //长度 多加一个字节的 CRC
RcvLEN(txbuf, rxbuf + 1, 1); //长度 多加一个字节的 CRC
uint8_t len = *(rxbuf + 1);
RcvData(txbuf, rxbuf + 2);
RcvData(txbuf, rxbuf + 2, len);
RcvSW(txbuf, rxbuf + 2 + rxbuf[1], 0x90);
RcvSW(txbuf, rxbuf + 2 + len, 0x90);
//计算接收到数据的CRC
if (CalcCRC7(rxbuf + 2, rxbuf[1] - 1) != rxbuf[rxbuf[1] + 1])
@ -263,19 +380,13 @@ CMD_RESEND:
}
}
printf("rx %1d bytes: ", rxbuf[1] + 4);
// printf("rx %1d bytes: ", rxbuf[1] + 4);
#ifdef __ANDROID__
__android_log_print(ANDROID_LOG_INFO, TAG_SPI, "rx %1d bytes:", rxbuf[1] + 4);
#endif
std::string result = "Random: ";
char output[16] = { 0 };
for (i = 0; i < rxbuf[1] + 4; i++) {
sprintf(output, " %02x ", rxbuf[i]);
result += output;
}
__android_log_print(ANDROID_LOG_INFO, TAG_SPI, "%s", result.c_str());
// printf("\n");
memcpy(output, rxbuf + 2, len);
return 0;
}
@ -289,6 +400,7 @@ std::string NrsecPort::Version()
int retval;
int msglen;
cnt = 0;
msglen = 5;
memset(rxbuf, 0, sizeof(rxbuf));
memset(txbuf, 0, sizeof(txbuf));
@ -330,7 +442,9 @@ CMD_RESEND:
}
//printf("rx %1d bytes: ", rxbuf[1]+4);
#ifdef __ANDROID__
__android_log_print(ANDROID_LOG_INFO, TAG_SPI, "rx %1d bytes:", rxbuf[1] + 4);
#endif
std::string version = "";
char output[16] = { 0 };
for (i = 0; i < dataLen; i++) {
@ -340,7 +454,9 @@ CMD_RESEND:
snprintf(output, sizeof(output), "%c", *(rxbuf + 2 + i));
version += output;
}
#ifdef __ANDROID__
__android_log_print(ANDROID_LOG_INFO, TAG_SPI, "Version: %s", version.c_str());
#endif
//__android_log_print(ANDROID_LOG_INFO, "SPi", "%s", rxbuf);
// printf("\n");
@ -390,6 +506,7 @@ int NrsecPort::SM2ExportPublicKey(int index, uint8_t result[])//导出公钥
int retval;
int msglen;
cnt = 0;
msglen = 5;
memset(rxbuf, 0, sizeof(rxbuf));
memset(txbuf, 0, sizeof(txbuf));
@ -428,7 +545,9 @@ CMD_RESEND:
}
//printf("rx %1d bytes: ", rxbuf[1]+4);
#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++) {
@ -449,6 +568,7 @@ int NrsecPort::SM2ExportPrivateKey(int index, uint8_t result[])//导出私钥
int retval;
int msglen;
cnt = 0;
msglen = 5;
memset(rxbuf, 0, sizeof(rxbuf));
memset(txbuf, 0, sizeof(txbuf));
@ -489,8 +609,9 @@ CMD_RESEND:
}
//printf("rx %1d bytes: ", rxbuf[1]+4);
#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];
@ -509,7 +630,7 @@ CMD_RESEND:
return 0;
}
int NrsecPort::SM2InportPublicKey(int index, const uint8_t new_key[])//外部公钥导入存放在01
int NrsecPort::SM2ImportPublicKey(int index, const uint8_t new_key[])//外部公钥导入存放在01
{
int i;
int cnt;
@ -547,7 +668,9 @@ CMD_RESEND:
RcvSW(txbuf, rxbuf + 1, 0x90);
std::string result = "InPub: success";
#ifdef __ANDROID__
__android_log_print(ANDROID_LOG_INFO, TAG_SPI, "%s", result.c_str());
#endif
//__android_log_print(ANDROID_LOG_INFO, "SPi", "%s", rxbuf);
// printf("\n");
@ -555,7 +678,7 @@ CMD_RESEND:
return 0;
}
int NrsecPort::SM2InportPrivateKey(int index, const uint8_t new_key[])//导入私钥 没测试
int NrsecPort::SM2ImportPrivateKey(int index, const uint8_t new_key[])//导入私钥 没测试
{
int i;
int cnt;
@ -593,7 +716,9 @@ CMD_RESEND:
RcvSW(txbuf, rxbuf + 1, 0x90);
std::string result = "InPri: success";
#ifdef __ANDROID__
__android_log_print(ANDROID_LOG_INFO, TAG_SPI, "%s", result.c_str());
#endif
return 0;
@ -623,7 +748,9 @@ int NrsecPort::SM3Hash(uint8_t *to_hash, int len, uint8_t *out_hash)//原始哈
}
SM3Hash_CMD[3] = re[2] + re[3] * 16;
SM3Hash_CMD[4] = re[0] + re[1] * 16;
#ifdef __ANDROID__
__android_log_print(ANDROID_LOG_INFO, "len", "len=%x", len);
#endif
//printf("tx %1d bytes: ", msglen);
//__android_log_print(ANDROID_LOG_INFO, "SPi", "tx %1d bytes", msglen);
@ -663,7 +790,7 @@ CMD_RESEND:
return 0;
}
int NrsecPort::sm3hash_tosm2(uint8_t *in, int inl, uint8_t *out, uint8_t *pubkey, uint8_t* pucID, int idl)
int NrsecPort::SM3Hash(uint8_t *in, int inl, uint8_t *out, uint8_t *pubkey, uint8_t* pucID, int idl)
{
int nRet, l;
uint8_t *Z = NULL;
@ -761,8 +888,9 @@ CMD_RESEND:
RcvData(txbuf, rxbuf + 2);
RcvSW(txbuf, rxbuf + 2 + rxbuf[1], 0x90);
#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];
@ -805,13 +933,14 @@ CMD_RESEND:
SendEnd(txbuf, rxbuf);
RcvSW(txbuf, rxbuf, 0x90);
#ifdef __ANDROID__
__android_log_print(ANDROID_LOG_INFO, "SPi", "rx %1d bytes:", rxbuf[1] + 4);
#endif
return 0;
}
int NrsecPort::SM2encrypt(int index, uint8_t *to_encrypt, uint8_t *out_encrypt)//加密
int NrsecPort::SM2Encrypt(int index, uint8_t *to_encrypt, uint8_t *out_encrypt)//加密
{
int i;
int cnt;
@ -864,7 +993,7 @@ CMD_RESEND:
return 0;
}
int NrsecPort::SM2decoder(int index, uint8_t *to_decoder, uint8_t *out_decoder)//解密
int NrsecPort::SM2Decrypt(int index, uint8_t *to_decoder, uint8_t *out_decoder)//解密
{
int i;
int cnt;
@ -935,10 +1064,7 @@ int NrsecPort::SM2cert(int type, int index, string cert, uint8_t *out_cert)//证
//printf("tx %1d bytes: ", msglen);
int certlen = cert.length();
uint8_t to_cert[certlen];
for (int x = 0; x < certlen; x++) {
to_cert[x] = cert[x];
}
SM2cert_CMD[2] = type;
SM2cert_CMD[3] = index;
SM2cert_CMD[4] = certlen;
@ -953,7 +1079,7 @@ CMD_RESEND:
SendId(txbuf, rxbuf, 0x55);
memcpy(txbuf, to_cert, certlen);
memcpy(txbuf, cert.c_str(), certlen);
SendData(txbuf, rxbuf, certlen);

@ -2,6 +2,7 @@
#define __NRSECPORT_H__
#include "SpiPort.h"
#include <mutex>
#define CMD_HEAD_SIZE 5
@ -11,20 +12,25 @@ typedef uint8_t uint8_t;
class NrsecPort : public SpiPort {
public:
int Spirandom();
int ImportIV(const uint8_t* iv, uint8_t ivLength);
int SM1Encrypt(const uint8_t* data, uint16_t dataLen, uint8_t* encryptedData, uint16_t bufferLen);
int SM1Decrypt(const uint8_t* encryptedData, uint16_t encryptedDataLen, uint8_t* data, uint16_t bufferLen);
int Random(uint8_t* output, uint8_t length);
std::string Version();
int Indentify(uint8_t *to_idt, uint8_t *out_idt);
int SM2keypair(int index);
int SM2ExportPublicKey(int index, uint8_t result[]);
int SM2ExportPrivateKey(int index, uint8_t result[]);
int SM2InportPublicKey(int index, const uint8_t new_key[]);
int SM2InportPrivateKey(int index, const uint8_t new_key[]);
int SM2ImportPublicKey(int index, const uint8_t new_key[]);
int SM2ImportPrivateKey(int index, const uint8_t new_key[]);
int SM3Hash(uint8_t *to_hash, int len, uint8_t *out_hash);
int sm3hash_tosm2(uint8_t *in, int inl, uint8_t *out, uint8_t *pubkey, uint8_t *pucID, int idl);
int SM3Hash(uint8_t *in, int inl, uint8_t *out, uint8_t *pubkey, uint8_t *pucID, int idl);
int SM2Sign(int index, const uint8_t *to_sign, uint8_t *out_sign);
int SM2VerifySign(int index, uint8_t *hash, uint8_t * vs);
int SM2encrypt(int index, uint8_t *to_encrypt, uint8_t * out_encrypt);
int SM2decoder(int index, uint8_t *to_decoder, uint8_t *out_decoder);
int SM2Encrypt(int index, uint8_t *to_encrypt, uint8_t * out_encrypt);
int SM2Decrypt(int index, uint8_t *to_decoder, uint8_t *out_decoder);
int SM2cert(int type, int index, string cert, uint8_t *out_cert);
protected:
@ -40,6 +46,8 @@ protected:
void SendId(uint8_t *txbuf, uint8_t *buf, uint8_t id);
void SendData(uint8_t *data, uint8_t *rxbuf, int data_size);
protected:
std::mutex m_mutex;
};

@ -47,6 +47,9 @@ Java_com_xinyingpower_testcomm_MainActivity_testSpi(
string b="C=CN,ST=jiangsu,L=nanjing,O=GDD,OU=nari,CN=test001";
a.Version();
uint8_t output[16] = { 0 };
a.Random(output, 16);
a.SM2keypair(0x00);
//a.SM3Hash(newkey,16, outpub);
//a.sm3hash_tosm2(newkey,16,outpub,newkey,pucid,16);

Loading…
Cancel
Save