Comments (2)
贴一下主要的代码,有没有大佬能指导一下
group = EC_GROUP_new_by_curve_name(NID_sm2p256v1);
if (ID.isEmpty()) {
idLen = 0;
ecKey = new_ec_key(group, priKey.toLocal8Bit().data(), NULL, NULL);
}
else {
if (pubKeyX.isEmpty() || pubKeyY.isEmpty()) {
mesg.critical(this, "错误", "公钥输入错误,请重新输入!", nullptr, nullptr);
goto END;
}
IDBin = OPENSSL_hexstr2buf(ID.toLocal8Bit().data(), &idLen);
ecKey = new_ec_key(group, priKey.toLocal8Bit().data(), pubKeyX.toLocal8Bit().data(), pubKeyY.toLocal8Bit().data());
}
if (ecKey == NULL) {
ulErr = getLastErr(szErrMsg);
qDebug() << szErrMsg;
mesg.critical(this, "错误", "输入参数错误,请重新输入!", nullptr, nullptr);
goto END;
}
if (!sm2_sig(ecKey,
IDBin, idLen,
inDataBin, inDataBinLen,
sigData, (int*)&sigDataLen)) {
ulErr = getLastErr(szErrMsg);
qDebug() << szErrMsg;
mesg.critical(this, "错误", "SM2签名错误!", nullptr, nullptr);
goto END;
}
/******************************************************/
EC_KEY MainWindow::new_ec_key(const EC_GROUP group,
const char sk, const char xP, const char yP)
{
int ok = 0;
EC_KEY ec_key = NULL;
BIGNUM d = NULL;
BIGNUM x = NULL;
BIGNUM y = NULL;
char *prikey = NULL;
if (group == NULL) {
goto end;
}
if (!(ec_key = EC_KEY_new())) {
goto end;
}
if (!EC_KEY_set_group(ec_key, group)) {
goto end;
}
if (sk) {
if (!BN_hex2bn(&d, sk)) {
goto end;
}
if (!EC_KEY_set_private_key(ec_key, d)) {
goto end;
}
}
if (xP && yP) {
if (!BN_hex2bn(&x, xP)) {
goto end;
}
if (!BN_hex2bn(&y, yP)) {
goto end;
}
if (!EC_KEY_set_public_key_affine_coordinates(ec_key, x, y)) {
goto end;
}
}
ok = 1;
end:
if (d) BN_free(d);
if (x) BN_free(x);
if (y) BN_free(y);
if (!ok && ec_key) {
ERR_print_errors_fp(stderr);
EC_KEY_free(ec_key);
ec_key = NULL;
}
return ec_key;
}
/***************************************************/
int MainWindow::sm2_sig(EC_KEY key,
unsigned char id, int id_len,
unsigned char in_data, int in_len,
unsigned char out_data, int out_len)
{
const EVP_MD id_md = EVP_sm3();
const EVP_MD msg_md = EVP_sm3();
unsigned char dgst[128] = { 0 };
size_t dgstlen = 128;
ECDSA_SIG sm2sig = NULL;
const BIGNUM sig_r;
const BIGNUM sig_s;
unsigned char* s_r = NULL;
unsigned char* s_s = NULL;
if (id_len != 0) {
if (!SM2_compute_message_digest(id_md, msg_md,
(const unsigned char*)in_data, in_len,
(const char*)id, id_len,
dgst, &dgstlen, key)) {
ulErr = getLastErr(szErrMsg);
qDebug() << szErrMsg;
return 0;
}
}
else {
memcpy(dgst, in_data, in_len);
dgstlen = in_len;
}
/* sign */
sm2sig = SM2_do_sign_ex(dgst, (int)dgstlen, NULL, NULL, key);
if (sm2sig == NULL) {
ulErr = getLastErr(szErrMsg);
qDebug() << szErrMsg;
return 0;
}
ECDSA_SIG_get0(sm2sig, &sig_r, &sig_s);
s_r = (unsigned char*)BN_bn2hex(sig_r);
s_s = (unsigned char*)BN_bn2hex(sig_s);
memcpy(out_data, s_r, 64);
memcpy(out_data + 64, s_s, 64);
*out_len = 128;
return 1;
}
/************************************************/
from gmssl.
格式不同,
memcpy(out_data, s_r, 64);
memcpy(out_data + 64, s_s, 64);
你看你的, 这个是 r+s的, gmssl 是asn1(r,s)
from gmssl.
Related Issues (20)
- 按 README 所说无法得到 Linux 下的静态库 HOT 1
- 3.0比2.0的并发加解密性能低?
- 3.0比2.0的并发加解密性能低?
- 官网 403
- 加密证书签发问题
- 请问3.1.1版本是否支持AIX HOT 2
- Python的ssl加载证书出错
- 国密 tomcat已经验证没问题, 使用iOS 的GmSSL最新sdk访问 localhost 报错,访问其他国密地址没有问题
- sm2.h文件报不存在 HOT 2
- 请求新加全局控制打印函数的功能,可以使得gmssl库不打屏 HOT 1
- 请求添加类似openssl 的 sslPending 的函数接口
- 请求添加 获取 TLS_CTX 、TLS_CONNECT 的size的接口
- sm2_encrypt_demo.c运行解密失败 HOT 2
- win 下面 golang cgo编译 报/x86_64-w64-mingw32/bin/ld.exe: cannot find -lgmssl
- 可否在sm2_signtest.c添加secp256r1曲线的相关代码? HOT 4
- linux sh安装后查看版本 报gmssl: error while loading shared libraries: libgmssl.so.3: cannot open shared object file: No such file or directory HOT 3
- 建议编译增加mingw的支持
- gmssl tls12_client 和 tls13_client 链接各自服务器报错,tlcp_client 链接 tlcp_server 没问题
- 私钥密码中包含"^"时无法使用sm2_private_key_info_decrypt_from_pem读取证书 HOT 2
- Gmssl V2最新版,ecb模式下,aes,des,sm4,加密中英文混合后数据均不完整但英文正常,cbc模式下中文英文混合均正常 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gmssl.