OpenSSL i2o ECPublicKey not working

0 votes

I have this code:

#include <stdio.h>

#include <openssl/sha.h>

#include <openssl/ssl.h>


int main(){

    printf("OpenSSL version: %s\n",OPENSSL_VERSION_TEXT);

    EC_KEY * key = EC_KEY_new_by_curve_name(NID_secp256k1);

    if(!EC_KEY_generate_key(key)){

        printf("GENERATE KEY FAIL\n");

        return 1;

    }

    u_int8_t pubSize = i2o_ECPublicKey(key, NULL);

    if(!pubSize){

        printf("PUB KEY TO DATA ZERO\n");

        return 1;

    }

    u_int8_t * pubKey = malloc(pubSize);

    if(i2o_ECPublicKey(key, &pubKey) != pubSize){

        printf("PUB KEY TO DATA FAIL\n");

        return 1;

    }

    u_int8_t * hash = malloc(SHA256_DIGEST_LENGTH);

    SHA256(pubKey, pubSize, hash);

    for (int x = 0; x < 32; x++) {

        printf("%.2x",hash[x]);

    }

    EC_KEY_free(key);

    free(pubKey);

    free(hash);

    return 0;

}

I get this error after second i2o_ECPublicKey:

(gdb) p/x *pubKey @ 65

Cannot access memory at address 0x4d0ff1

Please help.

Aug 17, 2018 in Blockchain by slayer
• 29,350 points

edited Nov 23, 2018 by Kalgi 916 views

1 answer to this question.

0 votes

When your right something in the buffer, the pointer is moved to point to the location at the end of what you have written. So, i2o_ECPublicKey moves the pointer by the number of bytes you have written into the buffer.

Try this code and it’ll work:


           u_int8_t * pubKey = malloc(pubSize);

    +       u_int8_t * pubKey2 = pubKey;

    -       if(i2o_ECPublicKey(key, &pubKey) != pubSize){

    +       if(i2o_ECPublicKey(key, &pubKey2) != pubSize){

                printf("PUB KEY TO DATA FAIL\n");
answered Aug 17, 2018 by digger
• 26,740 points

Related Questions In Blockchain

0 votes
1 answer

Geth 'sendTransaction' not working

When transactions are put in a loop, ...READ MORE

answered Jul 3, 2018 in Blockchain by Omkar
• 69,210 points
1,196 views
0 votes
1 answer

My first Hyperledger fabric network is not working

Delete as admin the channel-artifacts folder, down ...READ MORE

answered Aug 10, 2018 in Blockchain by Johnathon
• 9,090 points
1,628 views
0 votes
3 answers
0 votes
1 answer

Bitcoin payment system not working

I found the problem, it was actually ...READ MORE

answered Aug 30, 2018 in Blockchain by slayer
• 29,350 points
640 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,234 views
0 votes
1 answer

Invalid Batch or signature in Savtooth

This will solve your problem import org.apache.commons.codec.binary.Hex; Transaction txn ...READ MORE

answered Aug 1, 2018 in Blockchain by digger
• 26,740 points
726 views
+1 vote
1 answer
0 votes
1 answer

Function not working from class

Try this code:  // Add new block ...READ MORE

answered Aug 16, 2018 in Blockchain by digger
• 26,740 points
431 views
0 votes
1 answer

SteemJS not working in Firefox/IE

npm install adcpm/steem#dev Just build it from develop ...READ MORE

answered Sep 14, 2018 in Blockchain by digger
• 26,740 points
363 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP