Coder Social home page Coder Social logo

Comments (2)

wsstot avatar wsstot commented on June 12, 2024

贴一下主要的代码,有没有大佬能指导一下

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.

lpilp avatar lpilp commented on June 12, 2024

格式不同,

memcpy(out_data, s_r, 64);
memcpy(out_data + 64, s_s, 64);

你看你的, 这个是 r+s的, gmssl 是asn1(r,s)

from gmssl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.