I am trying to create a program that, after creating a public / private key pair using the OppenSSL EC function EC_KEY_generate_key, stores them in separate files and extracts them to generate an ECDH key.
My problem is that although I store them correctly (without any extra character), when I read the file and try to convert the hexadecimal characters to BIGNUM, the character β04β or β00β appears accidentally (or even sometimes not), Therefore, when I try to set public / private keys and verify the entire key, it fails. Can someone help me on this? Can key verification errors be caused by these characters, or are they normal?
Here is my code that generates / stores the private key (the same public):
EC_KEY *b = NULL; const BIGNUM *ppriv_b; FILE *claveprivb; const EC_GROUP *group; b = EC_KEY_new_by_curve_name(NID_X9_62_prime192v1); group = EC_KEY_get0_group(b); EC_KEY_generate_key(b); claveprivb = fopen("/tmp/mnt/claveprivb", "w+"); ppriv_b = EC_KEY_get0_private_key(b); if ((ppriv_b != NULL)) BN_print_fp(claveprivb,ppriv_b); fclose(claveprivb);
And here is my code to retrieve the private key:
int i, s, blen, bout, ret = 0; unsigned char *bbuf; FILE *clavepriv, *clavetotalb; const char cpriv_string[PRIVATE_KEY_SIZE]; BIGNUM *priv; EC_KEY *b = NULL; const EC_GROUP *groupb; b = EC_KEY_new_by_curve_name(NID_X9_62_prime192v1); groupb = EC_KEY_get0_group(b); //Open the file with the hexadecimals (PRIVATE KEY) clavepriv = fopen("/tmp/mnt/claveprivb", "r"); kk2 = fread(&cpriv_string, sizeof(char), PRIVATE_KEY_SIZE, clavepriv); priv = BN_new(); //THIS FUNCTION (HEX2BN) GENERATES THE RANDOM CHARACTER: kk2 = BN_hex2bn(&priv, cpriv_string); ret = EC_KEY_set_private_key(b, priv); //HERE I retrieve the public key by the same way and set it into EC_KEY b, //the same random character appears in the public key if (!EC_KEY_check_key(b)) { printf("EC_KEY_check_key failed\n"); } else { printf("Key verified OK\n"); } //It fails when try to check it. int k; clavetotalb = fopen("/tmp/mnt/clavetotalb", "w+"); k = EC_KEY_print_fp(clavetotalb, b, 0); bout = ECDH_compute_key(bbuf, blen, EC_KEY_get0_public_key(b), b, KDF1_SHA1);
Any advice would be greatly appreciated !!!! Thanks!!!
After I read the response post, I tried to use these methods to decode and encode the public key, but by the time I try to calculate the ECDH key, I get a segmentation error. The goal of my program is to generate two EC keys, write them to several files, and then extract them and calculate the ECDH key with them. This is a list of things that I am transferring from my original program to the first thread, please tell me something is wrong:
* Generate EC key (public & private) * Decode the private key with i2d_ECPrivatekey() * Decode the public key with i2o_ECPublickey() * Write them into several files. * Read the file with the public key. * Encode it with o2i_ECPublickey() * Read the file with the private key. * Encode it with d2i_ECPrivatekey(). * Compute the ECDH key.(Here is where I get the segmentation fault)
I am very tired of this OpenSSL library ... it is not available to first users ...