yebin-yu / openssh_with_ukey

Other
0 stars 0 forks source link

README #5

Closed yebin-yu closed 5 months ago

yebin-yu commented 5 months ago

How to compile with libgm3000.1.0.so

Copy so to /usr/lib

cp libgm3000.1.0.so /usr/lib/libgm3000.so

Edit LIB in Makefile

LIBS = -lgm3000
yebin-yu commented 5 months ago

UKEY - PubKey

BitLen: 256
x: 00000000000000000000000000000000a3787ede1dbc4e1c389c9ae2137cb123b7594e4ae2dd859b89e1ff9546521
y: 00000000000000000000000000000000981e2c635fb7295728aa5bccb18c49b71bf52eec226abc9152153f838fcbce

print code

void dumpBytesHex(const char *name, unsigned char *bytes, size_t bytesLen)
{
    const char *outName = (name != NULL) ? name : "Default:";
    printf("%s", outName);

    for (int i = 0; i < bytesLen; i++) {
        printf("%x", bytes[i]);
    }

    printf("\n");
}

    dumpBytesHex("buf: ", buf, sizeof(buf));
    printf("BitLen: %u\n", blob->BitLen);

    dumpBytesHex("x: ", blob->XCoordinate, (ECC_MAX_XCOORDINATE_BITS_LEN/8));
    dumpBytesHex("y: ", blob->YCoordinate, (ECC_MAX_XCOORDINATE_BITS_LEN/8));

    dumpBytesHex("r: ", stSign.r, sizeof(stSign.r));
    dumpBytesHex("s: ", stSign.s, sizeof(stSign.s));

res

buf: 010000000000000000000000000000000000a3787ede1dbc4e1c389c9ae2137cb123b7594e4ae2dd859b89e1ff954652100000000000000000000000000000000981e2c635fb7295728aa5bccb18c49b71bf52eec226abc9152153f838fcbce00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
BitLen: 256
x: 00000000000000000000000000000000a3787ede1dbc4e1c389c9ae2137cb123b7594e4ae2dd859b89e1ff9546521
y: 00000000000000000000000000000000981e2c635fb7295728aa5bccb18c49b71bf52eec226abc9152153f838fcbce
r: 0000000000000000000000000000000058ea4024fcf97039bec54d1421529d1992d70c3c3b1a11e7be6c23f7f986435
s: 000000000000000000000000000000006c9d731c80446747c4f43e21f34cd369f5fec037fb4762f192c38e9495395cf6
yebin-yu commented 5 months ago

sm2 AAAAA3NtMgAAAANzbTIAAABBBAAKN4ft4dvE4cOJya4hN8sSO3WU5K4t2Fm4nh/5VGUhAJgeLGNftylXKKpbzLGMSbcb9S7sImq8kVIVP4OPy84= alex@W00621009

yebin-yu commented 5 months ago

sm2_pubkey = 04|X|Y Openssh格式的公钥是: "sm2 " + base64.b64encode("00000003736d3200000003736d3200000041" + sm2_pubkey) + " alex@W00621009"

yebin-yu commented 5 months ago

sm2 AAAAA3NtMgAAAANzbTIAAABBBDFm8NOcCa/yaI7pVJuGpqbUuToMLREOl5+WhWYRV8FD/DDtRFPMiIdvWWHEG+71ZRc4Hagx93B3H05X/thNiOE= root@yebinyu sm2 AAAAA3NtMgAAAANzbTIAAABBBO80FQEtt+uuiPR6WyksZQgIoNUQwCijTA6VkgESIoPSF7KzWav6Ign7fSBQxYNt6E2q+laD7Zi5416glRnmuW8= root@yebinyu

yebin-yu commented 5 months ago
image
yebin-yu commented 5 months ago
EC_KEY * SM2_gmPub2OpensslPub(ECCPUBLICKEYBLOB *gmpub)
{
    unsigned char octs[65] = {0};
    octs[0] = POINT_CONVERSION_UNCOMPRESSED;

    unsigned char *x = &octs[1];
    unsigned char *y = &octs[33];
    memcpy(x, &gmpub->XCoordinate[32], 32);
    memcpy(y, &gmpub->YCoordinate[32], 32);

    EC_KEY *key = EC_KEY_new_by_curve_name(NID_sm2);
    if (key == NULL) {
        return NULL;
    }

    const EC_GROUP *group = EC_KEY_get0_group(key);
    if (group == NULL) {
        return NULL;
    }

    EC_POINT *pub = EC_POINT_new(group);
    if (pub == NULL) {
        return NULL;
    }

    if ( EC_POINT_oct2point(group, pub, octs, sizeof(octs), NULL) != 1 ) {
        return NULL;
    }

    if (EC_KEY_set_public_key(key, pub) != 1) {
        return NULL;
    }
    return key;
}
yebin-yu commented 5 months ago

unsigned char*SM2_gmSig2OpensslSig(ECCSIGNATUREBLOB *sigBlob, int *len)
{
    BIGNUM *rB=NULL;
    BIGNUM *sB=NULL;

    BYTE *r = sigBlob->r+32;
    BYTE *s = sigBlob->s+32;

    rB = BN_bin2bn(r, 32, NULL);
    if(rB == NULL) {return NULL;}
    sB = BN_bin2bn(s, 32, NULL);
    if(sB == NULL) {return NULL;}

    ECDSA_SIG *ecdsaSig = ECDSA_SIG_new();
    if (ecdsaSig == NULL) {return NULL;}

    if(ECDSA_SIG_set0(ecdsaSig, rB, sB) != 1) {
        return NULL;
    }

    unsigned char* sig = NULL;
    int sigLen = i2d_ECDSA_SIG(ecdsaSig, &sig);
    *len = sigLen;
    return sig;
}