1

(3 replies, posted in wolfCrypt)

@Kaleb

Thank you for the advice!

I had some time to loop back around and see what I can come up with.

I'm able to import the PrivateKey successfully, but am not able to generate Signatures deterministically.


I'm using RFC6979, and I am able to generate the nonce32 successfully.

The PrivateKey is now importing successfully.

Making the PublicKey appears to complete successfully as `wc_ecc_check_key` passes.

Signing and Verifying are successful, but the signatures are non-deterministic/change with every run.


No signatures have matched my test fixtures, which pass on libs like SECP256K1, BCrypto, BCL, etc.


Here's the example method I'm working on.

int signEcdsa(const uint8_t *hash32, const uint8_t* privateKeyBytes, uint8_t *outSignature) {
    WC_RNG      rng;
    byte*       keyBuf = (byte*)privateKeyBytes;
    ecc_key     keys;

    int ret     = 0;

    byte nonce32[32];
    nonce_function_rfc6979(nonce32,hash32, privateKeyBytes);
    // for (uint8_t i = 0U; i < 32; ++i) {
    //     printf("%02x", nonce32[i]);
    // }
    // nonce32: f698e12b30c6b7f3f44af3400c870911e8083061d7a0220df010efb97f560fc8


    ret = wc_InitRngNonce(&rng, nonce32, 32);
    printf("\nwc_InitRngNonce | ret: %d\n", ret);
    if (ret != 0) {
        return ret;
    }

    ret = wc_ecc_init(&keys);
    printf("\nwc_ecc_init | ret: %d\n", ret);
    if (ret != 0) {
        return ret;
    }

    ret = wc_ecc_import_private_key(privateKeyBytes, 32, NULL, 0, &keys);
    printf("\nwc_ecc_import_private_key | ret: %d\n", ret);
    if (ret != 0) {
        return ret;
    }


    // byte eccPrivKeyBuf[ECC_BUFSIZE];
    // word32 pkLen = 32;
    // ret = wc_ecc_export_private_only(&keys, eccPrivKeyBuf, &pkLen);
    // if (ret != 0) {
    //     printf("\nwc_ecc_export_private_only - ret: %d\n", ret);
    //     return ret;
    // }
    // for (uint8_t i = 0U; i < 32; ++i) {
    //     printf("%02x", eccPrivKeyBuf[i]);
    // }
    // privateKey Hex: d8839c2432bfd0a67ef10a804ba991eabba19f154a3d707917681d45822a5712


    ret = wc_ecc_make_pub(&keys, &keys.pubkey);
    printf("\nwc_ecc_make_pub | ret: %d\n", ret);
    if (ret != 0) {
        return ret;
    }

    ret = wc_ecc_check_key(&keys);
    printf("\nwc_ecc_check_key | ret: %d\n", ret);
    if (ret != 0) {
        return ret;
    }

    word32 sigLen = 72;
    ret = wc_ecc_sign_hash(hash32, 32, outSignature, &sigLen, &rng, &keys);
    printf("\nwc_ecc_sign_hash | ret %d | sigLen %d\n", ret, sigLen);
    if (ret != 0) {
        return ret;
    }

    int is_valid_sig = 0;
    ret = wc_ecc_verify_hash(outSignature, sigLen, hash32, 32, &is_valid_sig, &keys);
    printf("\nwc_ecc_verify_hash | ret %d | is_valid_sig %d\n", ret, is_valid_sig);
    if (ret != 0) {
        return ret;
    }

    wc_FreeRng(&rng);

    return ret;
}

There are a lot more checks that would need to be in place in a final implementation, but I have no idea where to go next.

Thank you again, and I'm open to any advice!!

2

(3 replies, posted in wolfCrypt)

Apologies if this has been answered here before..

I'm having trouble figuring out how to create an`ecc_key` from a Raw Koblitz/SECP256K1 PrivateKey 32-byte-array.

I'm able to generate new keypairs and perform Ecdsa signing operations just fine,
but I'm absolutely stumped on how I can get an `ecc_key` from a raw privateKey.

My use case doesn't involve cert files, and typically sees the privateKey being derived from a Sha256 hash of a given input.

e.g.
[code]
const char *passphrase = "this is a top secret passphrase";

// Sha256(passphrase) -> privateKey[32];

// pkHex = "d8839c2432bfd0a67ef10a804ba991eabba19f154a3d707917681d45822a5712"
const uint8_t privateKey[32] = { 216, 131, 156, 36, 50, 191, 208, 166, 126, 241, 10, 128, 75, 169, 145, 234, 187, 161, 159, 21, 74, 61, 112, 121, 23, 104, 29, 69, 130, 42, 87, 18 };

ecc_key key;

//  <-- Where my brain throws an exception and I stare blankly at the API