Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to generate Public Key from the X, Y component and Curve for ECDH? #85

Open
shahdhiren opened this issue Sep 1, 2016 · 3 comments

Comments

@shahdhiren
Copy link

I am trying to generate Public Key from the X, Y Component and Curve that I received from Server. Below code I am using but not able to generate Public Key.

void* getPublicKeyFromX_Y(const void *xInput, int xLen, const void *yInput, int yLen, int outputLength)
{
    EC_GROUP *group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
    BN_CTX *ctx = BN_CTX_new();
    EC_POINT *point = EC_POINT_new(group);
    void *pubKey = NULL;

    BIGNUM *x = BN_bin2bn(xInput, xLen, NULL);
    BIGNUM *y = BN_bin2bn(yInput, yLen, NULL);


    if(EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx))
    {
        BIGNUM publicKeyBn;
        BN_init(&publicKeyBn);
        EC_POINT_point2bn(group, point, POINT_CONVERSION_UNCOMPRESSED, &publicKeyBn, NULL);
        int length = 65; //Uncompressed, For compressed, it should be 33

        unsigned int offset = length - BN_num_bytes(&publicKeyBn);
        pubKey = calloc(1, length * sizeof(char *));
        outputLength = length;
        BN_bn2bin(&publicKeyBn, pubKey+offset);
        BN_clear(&publicKeyBn);
        EC_GROUP_free(group);
//        EC_KEY *publicKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
//        if(EC_KEY_set_public_key(publicKey, point))
//        {
//            printf("here");
//        }
    }
    BN_free(x);
    BN_free(y);

    return pubKey;

}

+(NSData *)getPublicKeyFromX:(NSData *)xInput Y:(NSData *)yInput
{
    NSData *pubKey = nil;

    int outputLength = 65;
    void *pubkey = getPublicKeyFromX_Y([xInput bytes],(int)[xInput length],[yInput bytes],(int)[yInput length], outputLength);

    pubKey = [[NSData alloc] initWithBytes:pubkey length:outputLength];
    return pubKey;
}

Later I found this Library and was exploring my solution but didn't found any thing related.

Does CoreBitcoin contains my solution? How and from where should I attack my problem?

If this question is irrelevant for CoreBitcoin, please feel free to close. But I found it to be somewhat related and hence posting it to find my solution.

Help is much appreciated. Thanks.

@dtco-kimi
Copy link

dtco-kimi commented Sep 5, 2017

Hi Shahdhiren

@shahdhiren
Did you find any solutions for your question? I also need to generate ECDH share secret. It seems like CoreBitcoin doesn't support this, but I have no idea how to use openssl to do this. Could you give me some suggestions? I also use Obj-C on ios.

Thanks.

@brett-doffing
Copy link

Not exactly sure how you are looking to use ECDH, but there is ECDH functionality for a BTCKey. For example I used...
BTCKey *DH_NotificationTXKey = [bobNotificationKey diffieHellmanWithPrivateKey:aliceUnassociatedKey];

@liunina
Copy link

liunina commented Oct 11, 2018

Not exactly sure how you are looking to use ECDH, but there is ECDH functionality for a BTCKey. For example I used...
BTCKey *DH_NotificationTXKey = [bobNotificationKey diffieHellmanWithPrivateKey:aliceUnassociatedKey];

why the function "diffieHellmanWithPrivateKey" require bobNotificationKey and aliceUnassociatedKey is Privatekey? I was Confused.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants