Bitcoin Forum
April 16, 2024, 11:27:05 PM *
News: Latest Bitcoin Core release: 26.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: i2o_ECPublicKey Not Working...  (Read 1618 times)
MatthewLM (OP)
Legendary
*
Offline Offline

Activity: 1190
Merit: 1004


View Profile
June 07, 2012, 07:48:04 PM
Last edit: June 16, 2012, 04:51:29 PM by MatthewLM
 #1

Hello. To get anywhere with my bitcoin library I clearly need to have a cryptography library working. OpenSSL is playing up big time.

I have this test program:

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;
}

But i2o_ECPublicKey corrupts the pubKey memory block. See here: http://stackoverflow.com/questions/10906524/openssl-i2o-ecpublickey-not-working

I'm using OpenSSL 1.0.1c.

Until this is sorted there is a major problem with developing cbitcoin.

Thanks.
1713310025
Hero Member
*
Offline Offline

Posts: 1713310025

View Profile Personal Message (Offline)

Ignore
1713310025
Reply with quote  #2

1713310025
Report to moderator
1713310025
Hero Member
*
Offline Offline

Posts: 1713310025

View Profile Personal Message (Offline)

Ignore
1713310025
Reply with quote  #2

1713310025
Report to moderator
1713310025
Hero Member
*
Offline Offline

Posts: 1713310025

View Profile Personal Message (Offline)

Ignore
1713310025
Reply with quote  #2

1713310025
Report to moderator
"I'm sure that in 20 years there will either be very large transaction volume or no volume." -- Satoshi
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1713310025
Hero Member
*
Offline Offline

Posts: 1713310025

View Profile Personal Message (Offline)

Ignore
1713310025
Reply with quote  #2

1713310025
Report to moderator
MatthewLM (OP)
Legendary
*
Offline Offline

Activity: 1190
Merit: 1004


View Profile
June 07, 2012, 09:06:25 PM
 #2

Well I verified that the dylib and a files were for OpenSSL 1.0.1c by searching for the version in the files. Indeed I had libcrypto and libssl files for 1.0.1c. So I tried adding them to Xcode. First I ensured the -lssl and -lcrypto flags were removed and I tried the dylibs first. This is what I got:

Code:
warning: Unable to read symbols for /usr/local/ssl/lib/libcrypto.1.0.0.dylib (file not found).
warning: Unable to read symbols from "libcrypto.1.0.0.dylib" (not yet mapped into memory).
warning: Unable to read symbols for /usr/local/ssl/lib/libssl.1.0.0.dylib (file not found).
warning: Unable to read symbols from "libssl.1.0.0.dylib" (not yet mapped into memory).
[Switching to process 1266 thread 0x0]
dyld: Library not loaded: /usr/local/ssl/lib/libcrypto.1.0.0.dylib
  Referenced from: /Users/matt/Library/Developer/Xcode/DerivedData/cbitcoin-bdkcvvuopgevvwcciljkluzxqser/Build/Products/Debug/testKeyHash
  Reason: image not found

I found this odd. Surely the dynamic libraries can be loaded from any directory?

Well I tried moving them to /usr/local/ssl/lib/ and I get this:

Code:
OpenSSL version: OpenSSL 0.9.8r 8 Feb 2011

Erm... So a 1.0.1c dylib that gives the version 0.9.8r? Clearly something is corrupted. I get similar problems with the static libraries. "0.9.8r" is not anywhere in the library files but "1.0.1c" is. Does anyone know what on earth is going on?
2112
Legendary
*
Offline Offline

Activity: 2128
Merit: 1065



View Profile
June 07, 2012, 10:06:35 PM
 #3

Does anyone know what on earth is going on?
"man dyld" knows. In the other thread about pointers to functions I expressed the suspiction that you simply don't understand the concepts of dynamic linking. Now I have a confirmation of that suspiction.

I'm so sorry.

Please comment, critique, criticize or ridicule BIP 2112: https://bitcointalk.org/index.php?topic=54382.0
Long-term mining prognosis: https://bitcointalk.org/index.php?topic=91101.0
MatthewLM (OP)
Legendary
*
Offline Offline

Activity: 1190
Merit: 1004


View Profile
June 07, 2012, 10:10:53 PM
 #4

I must be dumb for not realising OPENSSL_VERSION_TEXT is a macro so it is being received by the old headers. I'll include the correct headers and look again later.

But no doubt the right library binaries are being loaded.

And yes I've always hated this linking nonsense. Tongue
MatthewLM (OP)
Legendary
*
Offline Offline

Activity: 1190
Merit: 1004


View Profile
June 08, 2012, 04:09:56 PM
 #5

@2112: I think I know what you meant now. To use weak linking? I might do that actually.

I haven't had time to sort out the OpenSSL headers yet but I still think the i2o_ECPublicKey won't work. If anyone understands it better please explain what might be wrong. Maybe the correct headers suddenly solves everything but I think that is a long shot.
MatthewLM (OP)
Legendary
*
Offline Offline

Activity: 1190
Merit: 1004


View Profile
June 16, 2012, 04:49:45 PM
 #6

I've spent the last week working on testing the scripts and improving the code quote dramatically.

Now I've been able to install OpenSSL 1.0.1c successfully and link it correctly but...

It still doesn't work. Does anyone know why i2o_ECPublicKey doesn't work? It works in the C++ bitcoin client so why not my code?

http://stackoverflow.com/questions/10906524/openssl-i2o-ecpublickey-not-working
error
Hero Member
*****
Offline Offline

Activity: 588
Merit: 500



View Profile
June 16, 2012, 06:07:30 PM
 #7

I've spent the last week working on testing the scripts and improving the code quote dramatically.

Now I've been able to install OpenSSL 1.0.1c successfully and link it correctly but...

It still doesn't work. Does anyone know why i2o_ECPublicKey doesn't work? It works in the C++ bitcoin client so why not my code?

http://stackoverflow.com/questions/10906524/openssl-i2o-ecpublickey-not-working

I know why it doesn't work and I posted a solution for you. Enjoy.

3KzNGwzRZ6SimWuFAgh4TnXzHpruHMZmV8
MatthewLM (OP)
Legendary
*
Offline Offline

Activity: 1190
Merit: 1004


View Profile
June 16, 2012, 06:13:40 PM
 #8

Thank you!

Should have figured that myself...
error
Hero Member
*****
Offline Offline

Activity: 588
Merit: 500



View Profile
June 16, 2012, 06:19:56 PM
 #9

Woohoo, I finally got some reputation on SO...

3KzNGwzRZ6SimWuFAgh4TnXzHpruHMZmV8
MatthewLM (OP)
Legendary
*
Offline Offline

Activity: 1190
Merit: 1004


View Profile
June 16, 2012, 07:07:25 PM
 #10

I'll give you the 100 reputation bounty tomorrow when it lets me.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!