|
|
|
@ -898,6 +898,7 @@ int SpiLIb::SM2Sign(int index,const unsigned char *to_sign,unsigned char *out_si
|
|
|
|
|
|
|
|
|
|
//printf("tx %1d bytes: ", msglen);
|
|
|
|
|
//__android_log_print(ANDROID_LOG_INFO, "SPi", "tx %1d bytes", msglen);
|
|
|
|
|
SM2Sign_CMD[3]=index;
|
|
|
|
|
|
|
|
|
|
CMD_RESEND:
|
|
|
|
|
|
|
|
|
@ -964,6 +965,7 @@ int SpiLIb::SM2VerifySign(int index, unsigned char *hash,unsigned char *vs)//SM2
|
|
|
|
|
|
|
|
|
|
//printf("tx %1d bytes: ", msglen);
|
|
|
|
|
//__android_log_print(ANDROID_LOG_INFO, "SPi", "tx %1d bytes", msglen);
|
|
|
|
|
SM2VerifySign_CMD[3]=index;
|
|
|
|
|
|
|
|
|
|
CMD_RESEND:
|
|
|
|
|
|
|
|
|
@ -991,3 +993,143 @@ int SpiLIb::SM2VerifySign(int index, unsigned char *hash,unsigned char *vs)//SM2
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int SpiLIb::SM2encrypt(int index,unsigned char *to_encrypt ,unsigned char *out_encrypt)//加密
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
int cnt;
|
|
|
|
|
unsigned char txbuf[512];
|
|
|
|
|
unsigned char rxbuf[512];
|
|
|
|
|
|
|
|
|
|
int retval;
|
|
|
|
|
int msglen;
|
|
|
|
|
int fd;
|
|
|
|
|
const char *devname = "/dev/spidevSE";
|
|
|
|
|
//const char *devname = "/dev/spidev0.0";
|
|
|
|
|
//const char *devname = "/dev/mtkgpioctrl";
|
|
|
|
|
|
|
|
|
|
// NrsecSpiPort spi("/dev/spidevSE");
|
|
|
|
|
//
|
|
|
|
|
// // NrsecSpiPort spi("/dev/spidev0.0")
|
|
|
|
|
fd = open(devname, O_RDWR);
|
|
|
|
|
if (fd < 0) {
|
|
|
|
|
perror("open");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
spi_master_init(devname, fd);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
msglen = 5;
|
|
|
|
|
memset(rxbuf, 0, sizeof(rxbuf));
|
|
|
|
|
memset(txbuf, 0, sizeof(txbuf));
|
|
|
|
|
|
|
|
|
|
//printf("tx %1d bytes: ", msglen);
|
|
|
|
|
//__android_log_print(ANDROID_LOG_INFO, "SPi", "tx %1d bytes", msglen);
|
|
|
|
|
SM2encrypt_CMD[3]=index;
|
|
|
|
|
|
|
|
|
|
CMD_RESEND:
|
|
|
|
|
|
|
|
|
|
memcpy(txbuf, (const void *) SM2encrypt_CMD, sizeof(SM2encrypt_CMD));
|
|
|
|
|
|
|
|
|
|
SendCmdHeader(fd, txbuf, rxbuf);
|
|
|
|
|
|
|
|
|
|
RcvINS(fd,txbuf,rxbuf,txbuf[1]);
|
|
|
|
|
|
|
|
|
|
SendId(fd,txbuf,rxbuf, 0x55);
|
|
|
|
|
|
|
|
|
|
memcpy(txbuf, to_encrypt, 32);
|
|
|
|
|
|
|
|
|
|
SendData(fd, txbuf, rxbuf,32);
|
|
|
|
|
|
|
|
|
|
SendEnd(fd,txbuf,rxbuf);
|
|
|
|
|
|
|
|
|
|
memcpy(txbuf, (const void *) SM2encrypt_CMD, sizeof(SM2encrypt_CMD));
|
|
|
|
|
|
|
|
|
|
RcvINS(fd,txbuf,rxbuf,txbuf[1]);
|
|
|
|
|
|
|
|
|
|
RcvLEN(fd,txbuf,rxbuf+1, 1);
|
|
|
|
|
|
|
|
|
|
RcvData(fd, txbuf, rxbuf+2);
|
|
|
|
|
|
|
|
|
|
RcvSW(fd, 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];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
int SpiLIb::SM2decoder(int index,unsigned char *to_decoder ,unsigned char *out_decoder)//解密
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
int cnt;
|
|
|
|
|
unsigned char txbuf[512];
|
|
|
|
|
unsigned char rxbuf[512];
|
|
|
|
|
|
|
|
|
|
int retval;
|
|
|
|
|
int msglen;
|
|
|
|
|
int fd;
|
|
|
|
|
const char *devname = "/dev/spidevSE";
|
|
|
|
|
//const char *devname = "/dev/spidev0.0";
|
|
|
|
|
//const char *devname = "/dev/mtkgpioctrl";
|
|
|
|
|
|
|
|
|
|
// NrsecSpiPort spi("/dev/spidevSE");
|
|
|
|
|
//
|
|
|
|
|
// // NrsecSpiPort spi("/dev/spidev0.0")
|
|
|
|
|
fd = open(devname, O_RDWR);
|
|
|
|
|
if (fd < 0) {
|
|
|
|
|
perror("open");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
spi_master_init(devname, fd);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
msglen = 5;
|
|
|
|
|
memset(rxbuf, 0, sizeof(rxbuf));
|
|
|
|
|
memset(txbuf, 0, sizeof(txbuf));
|
|
|
|
|
|
|
|
|
|
//printf("tx %1d bytes: ", msglen);
|
|
|
|
|
//__android_log_print(ANDROID_LOG_INFO, "SPi", "tx %1d bytes", msglen);
|
|
|
|
|
SM2decoder_CMD[3]=index;
|
|
|
|
|
|
|
|
|
|
CMD_RESEND:
|
|
|
|
|
|
|
|
|
|
memcpy(txbuf, (const void *) SM2decoder_CMD, sizeof(SM2decoder_CMD));
|
|
|
|
|
|
|
|
|
|
SendCmdHeader(fd, txbuf, rxbuf);
|
|
|
|
|
|
|
|
|
|
RcvINS(fd,txbuf,rxbuf,txbuf[1]);
|
|
|
|
|
|
|
|
|
|
SendId(fd,txbuf,rxbuf, 0x55);
|
|
|
|
|
|
|
|
|
|
memcpy(txbuf, to_decoder, 128);
|
|
|
|
|
|
|
|
|
|
SendData(fd, txbuf, rxbuf,128);
|
|
|
|
|
|
|
|
|
|
SendEnd(fd,txbuf,rxbuf);
|
|
|
|
|
|
|
|
|
|
memcpy(txbuf, (const void *) SM2decoder_CMD, sizeof(SM2decoder_CMD));
|
|
|
|
|
|
|
|
|
|
RcvINS(fd,txbuf,rxbuf,txbuf[1]);
|
|
|
|
|
|
|
|
|
|
RcvLEN(fd,txbuf,rxbuf+1, 1);
|
|
|
|
|
|
|
|
|
|
RcvData(fd, txbuf, rxbuf+2);
|
|
|
|
|
|
|
|
|
|
RcvSW(fd, 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];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|