From 5451ae869b9ec5444d88a14472e394758c5a4734 Mon Sep 17 00:00:00 2001 From: "XI.CHEN" Date: Wed, 6 Dec 2023 16:55:40 +0800 Subject: [PATCH] =?UTF-8?q?sm3hash=5Ftosm2()=20=E6=A0=88=E6=BA=A2=E5=87=BA?= =?UTF-8?q?=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/cpp/SpiLib.cpp | 10 +++++----- app/src/main/cpp/SpiLib.h | 4 ++-- app/src/main/cpp/native-lib.cpp | 29 ++++++++++++++++++----------- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/app/src/main/cpp/SpiLib.cpp b/app/src/main/cpp/SpiLib.cpp index f2c0663..3096434 100644 --- a/app/src/main/cpp/SpiLib.cpp +++ b/app/src/main/cpp/SpiLib.cpp @@ -732,12 +732,12 @@ int SpiLIb::SM2InPri(int index,const unsigned char new_key[])//导入私钥 没 return 0; } -int SpiLIb::SM3Hash(const unsigned char *to_hash ,int len,unsigned char *out_hash)//原始哈希 跑通了,没有验证 +int SpiLIb::SM3Hash(unsigned char *to_hash ,int len,unsigned char *out_hash)//原始哈希 跑通了,没有验证 { int i; int cnt; - unsigned char txbuf[256]; - unsigned char rxbuf[256]; + unsigned char txbuf[512]; + unsigned char rxbuf[512]; int retval; int msglen; @@ -799,9 +799,9 @@ int SpiLIb::SM3Hash(const unsigned char *to_hash ,int len,unsigned char *out_has RcvData(fd, txbuf, rxbuf+2); - RcvSW(fd, txbuf, rxbuf+2+rxbuf[1], 0x90);//? + RcvSW(fd, txbuf, rxbuf+2+rxbuf[1], 0x90); - __android_log_print(ANDROID_LOG_INFO, "SPi", "rx %1d bytes:", rxbuf[1]+4); + //__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]; diff --git a/app/src/main/cpp/SpiLib.h b/app/src/main/cpp/SpiLib.h index 30ac94c..00f4353 100644 --- a/app/src/main/cpp/SpiLib.h +++ b/app/src/main/cpp/SpiLib.h @@ -12,11 +12,11 @@ public: int SM2OutPri(int index,unsigned char result[]); int SM2InPub(int index,const unsigned char new_key[]); int SM2InPri(int index,const unsigned char new_key[]); - int SM3Hash(const unsigned char *to_hash ,int len,unsigned char *out_hash); + int SM3Hash(unsigned char *to_hash ,int len,unsigned char *out_hash); int sm3hash_tosm2(unsigned char *in,int inl,unsigned char *out, unsigned char *pubkey, unsigned char *pucID, int idl); int SM2Sign(int index,const unsigned char *to_sign,unsigned char *out_sign); - int SM2VerifySign(int index,unsigned char *hash,unsigned char *vs) + int SM2VerifySign(int index,unsigned char *hash,unsigned char *vs); diff --git a/app/src/main/cpp/native-lib.cpp b/app/src/main/cpp/native-lib.cpp index d2b5db0..a1ebc1f 100644 --- a/app/src/main/cpp/native-lib.cpp +++ b/app/src/main/cpp/native-lib.cpp @@ -9,8 +9,6 @@ #include #include -int testSpi(); -int testVersion(); class NrsecSpiPort : public SpiPort { @@ -73,22 +71,31 @@ Java_com_xinyingpower_testcomm_MainActivity_testSpi( jobject /* this */, jint port) { //testSpi(); SpiLIb a; - const unsigned char newkey[64]={0xaf,0x0c,0xa9,0x40,0x1f,0xe6,0xee,0x0f,0x4c, + unsigned char newkey[64]={0xaf,0x0c,0xa9,0x40,0x1f,0xe6,0xee,0x0f,0x4c, 0xfb,0xf7,0x17,0x71,0xde,0x61,0x59 -// ,0x0a,0x05,0x77, -// 0xfa,0xe7,0xd1,0x8d,0x10,0x3a,0x79,0x23,0xf2,0xb3, -// 0x6d,0xea,0x8e,0xe0,0x64,0xe7,0x5d,0x49,0x84,0xe4, -// 0x5f,0xc9,0x07,0x03,0x52,0x33,0x79,0x87,0xd4,0x62, -// 0x62,0xc0,0xcc,0xf0,0xd6,0x85,0x20,0x7f,0x7a,0xe8, -// 0xc8,0xed,0x12,0xdb,0xdc + ,0x0a,0x05,0x77, + 0xfa,0xe7,0xd1,0x8d,0x10,0x3a,0x79,0x23,0xf2,0xb3, + 0x6d,0xea,0x8e,0xe0,0x64,0xe7,0x5d,0x49,0x84,0xe4, + 0x5f,0xc9,0x07,0x03,0x52,0x33,0x79,0x87,0xd4,0x62, + 0x62,0xc0,0xcc,0xf0,0xd6,0x85,0x20,0x7f,0x7a,0xe8, + 0xc8,0xed,0x12,0xdb,0xdc }; - unsigned char outpub[32]; + unsigned char outpub[32],outsign[64]; + unsigned char pucid[16]={0x01,0x01,0x01,0x01,0x01,0x01,0x01, + 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01}; //a.SM2keypair(0x00); - a.SM3Hash(newkey,16, outpub); + //a.SM3Hash(newkey,16, outpub); + a.sm3hash_tosm2(newkey,16,outpub,newkey,pucid,16); + a.SM2Sign(0x00,outpub,outsign); + a.SM2VerifySign(0x00,outpub,outsign); for (int i = 0; i < 32; i++) { //sprintf(output, " %02x ", rxbuf[i]); __android_log_print(ANDROID_LOG_INFO, "SPi", "%02x", outpub[i]); } + for (int i = 0; i < 64; i++) { + //sprintf(output, " %02x ", rxbuf[i]); + __android_log_print(ANDROID_LOG_INFO, "SPi", "%02x", outsign[i]); + } //a.SM3Hash(0x00,0x10,newkey); //testVersion();