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.