user-0 提问: 国密SM2算法代码结果有错误，求大神看看

回复:
user: user-1
content: 仅供参考：void HexDump(char *buf,int len,int addr) {     int i,j,k;     char binstr[80];      for (i=0;i<len;i++) {         if (0==(i%16)) {             sprintf(binstr,"%08x -",i+addr);             sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);         } else if (15==(i%16)) {             sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);             sprintf(binstr,"%s  ",binstr);             for (j=i-15;j<=i;j++) {                 sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');             }             printf("%s\n",binstr);         } else {             sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);         }     }     if (0!=(i%16)) {         k=16-(i%16);         for (j=0;j<k;j++) {             sprintf(binstr,"%s   ",binstr);         }         sprintf(binstr,"%s  ",binstr);         k=16-k;         for (j=i-k;j<i;j++) {             sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');         }         printf("%s\n",binstr);     } }

user: user-2
content: 楼主，你的sm2签名 算出来的签名值是多少字节，你仔细算了吗   sm2签名值的长度是64个字节  你看下你这个是多少字节  ============== 你这个代码，网上一大片，但不对  不知道你有没解决这个问题

user: user-3
content: 怎么把SM2算法拆分成，生成公私钥对，用私钥签名，公钥验证这三个函数

user: user-4
content: 强制类型转换即可，如第一个void *" 类型的值分配到 "unsigned char *"，直接改为 if ((signature =(unsigned char*) OPENSSL_malloc(sig_len)) == NULL) 其他的类似

user: user-5
content: 适当的强制转换。

user: user-0
content: if (!BN_hex2bn(&z, "6CB28D99385C175C94F94E934817663FC176D925DD72B727260DBAAE1FB2F96F")) ABORT; 	if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) 	{ 		fprintf(stdout, " failed\n"); 		goto builtin_err; 	} 	if (!EC_POINT_get_affine_coordinates_GFp(group,Q, x, y, ctx)) 	{ 		fprintf(stdout, " failed\n"); 		goto builtin_err; 	} 	fprintf(stdout, "\nTesting K Point\n     x = 0x"); 	BNPrintf(x); 	fprintf(stdout, "\n     y = 0x"); 	BNPrintf( y); 	fprintf(stdout, "\n");  	EC_GROUP_get_order(group, order, ctx); 	if (!BN_nnmod(rp, x, order, ctx)) 	{ 		fprintf(stdout, " failed\n"); 		goto builtin_err; 	} 	if (!BN_copy(kinv, z )) 	{ 		fprintf(stdout, " failed\n"); 		goto builtin_err; 	}  // 	for(i=0;i<32;i++) // 		printf("%02X",digest[i]); // 	printf("\n");  	if (!SM2_sign_ex(1, digest, 32, signature, &sig_len, kinv, rp, eckey)) 	{ 		fprintf(stdout, " failed\n"); 		goto builtin_err; 	} 	fprintf(stdout, "ECSign OK\n");  	/* verify signature */ 	if (SM2_verify(1, digest, 32, signature, sig_len, eckey) != 1) 	{ 		fprintf(stdout, " failed\n"); 		goto builtin_err; 	} 	fprintf(stdout, "ECVerify OK\n     r = 0x"); 	d2i_ECDSA_SIG(&ecsig, &signature, sig_len); 	BNPrintf(ecsig->r); 	fprintf(stdout,"\n     s = 0x"); 	BNPrintf(ecsig->s); 	fprintf(stdout,"\n");  	//testing SM2DH vector 	/* create key */ 	if (!BN_hex2bn(&z, "6FCBA2EF9AE0AB902BC3BDE3FF915D44BA4CC78F88E2F8E7F8996D3B8CCEEDEE")) ABORT; 	if (!EC_POINT_mul(group,P, z, NULL, NULL, ctx)) ABORT; 	if (!EC_POINT_get_affine_coordinates_GFp(group,P, x, y, ctx)) ABORT; 	fprintf(stdout, "\nTesting A Key Point\n     x = 0x"); 	BNPrintf(x); 	fprintf(stdout, "\n     y = 0x"); 	BNPrintf( y); 	fprintf(stdout, "\n"); 	EC_KEY_set_private_key(eckey,z); 	EC_KEY_set_public_key(eckey, P);  	if (!BN_hex2bn(&z, "5E35D7D3F3C54DBAC72E61819E730B019A84208CA3A35E4C2E353DFCCB2A3B53")) ABORT; 	if (!EC_POINT_mul(group,Q, z, NULL, NULL, ctx)) ABORT; 	if (!EC_POINT_get_affine_coordinates_GFp(group,Q, x, y, ctx)) ABORT; 	fprintf(stdout, "\nTesting B Key Point\n     x = 0x"); 	BNPrintf(x); 	fprintf(stdout, "\n     y = 0x"); 	BNPrintf( y); 	fprintf(stdout, "\n"); 	//EC_KEY_set_private_key(eckey,z); 	//EC_KEY_set_public_key(eckey, P);  	if (!BN_hex2bn(&z, "33FE21940342161C55619C4A0C060293D543C80AF19748CE176D83477DE71C80")) ABORT; 	if (!EC_POINT_mul(group,P, z, NULL, NULL, ctx)) ABORT; 	if (!EC_POINT_get_affine_coordinates_GFp(group,P, x, y, ctx)) ABORT; 	fprintf(stdout, "\nTesting Rb Key Point\n     x = 0x"); 	BNPrintf(x); 	fprintf(stdout, "\n     y = 0x"); 	BNPrintf( y); 	fprintf(stdout, "\n");  	if (!BN_hex2bn(&z, "83A2C9C8B96E5AF70BD480B472409A9A327257F1EBB73F5B073354B248668563")) ABORT; 	if (!EC_POINT_mul(group,R, z, NULL, NULL, ctx)) ABORT; 	if (!EC_POINT_get_affine_coordinates_GFp(group,R, x, y, ctx)) ABORT; 	fprintf(stdout, "\nTesting Ra Key Point\n     x = 0x"); 	BNPrintf(x); 	fprintf(stdout, "\n     y = 0x"); 	BNPrintf( y); 	fprintf(stdout, "\n");      	SM2_DH_key(group,P, Q, z,eckey,outkey,keylen);  	fprintf(stdout,"\nExchange key --KDF(Xv||Yv)--  :"); 	for(i=0; i<outlen; i++) 		printf("%02X",outkey[i]); 	printf("\n");    builtin_err:	 	OPENSSL_free(signature); 	signature = NULL; 	EC_POINT_free(P); 	EC_POINT_free(Q); 	EC_POINT_free(R); 	EC_POINT_free(DHPoint); 	EC_KEY_free(eckey); 	eckey = NULL; 	EC_GROUP_free(group); 	BN_CTX_free(ctx); 	return 0;  }  int main() { 	CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0); 	CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); 	ERR_load_crypto_strings(); 	RAND_seed(rnd_seed, sizeof rnd_seed);  	 	SM2_Test_Vecotor2(); 	 	 	CRYPTO_cleanup_all_ex_data(); 	ERR_free_strings(); 	ERR_remove_state(0); 	CRYPTO_mem_leaks_fp(stderr); 	 	return 0;  }