[openssl-users] Help on basic EC programming

Matt Loah matt.loah at gmail.com
Tue Nov 24 18:46:12 UTC 2015


Hello all guys,

I'm a newbie... and going to understand the OpenSSL and the APIs involved
in, and even if I searched in the Net, there are two main question/subject
fields that I'd like to ask for.

1. Firstly, I wrote a little code that I don't know if it's really good
enough. So, comments, suggestions... will be greatly appreciated.

----
#include <iostream>
#include <openssl/ec.h>
#include <openssl/pem.h>
#include <openssl/err.h>

#define ECCTYPE "brainpoolP512t1"

int generate_keys (EC_KEY * ecc, EVP_PKEY * pkey)
{
    if (EC_KEY_generate_key (ecc) <= 0)
        return 1;
    else
    {
        if (EVP_PKEY_assign_EC_KEY (pkey, ecc) <= 0)
            return 2;
        else
        {
            BIO * bp_public  = BIO_new_file ("./key.pub.pem", "w+");
            BIO * bp_private = BIO_new_file ("./key.prv.pem", "w+");

            if (bp_public)
            {
                if (PEM_write_bio_PUBKEY (bp_public, pkey) <= 0)
                    return 3;
                else
                    BIO_free_all (bp_public);
            }
            else
                return 4;

            if (bp_private)
            {
                if (PEM_write_bio_PrivateKey (bp_private, pkey, nullptr,
nullptr, 0, 0, nullptr) <= 0)
                    return 5;
                else
                    BIO_free_all (bp_private);
            }
            else
                return 6;
        }
    }

    return 0;
}

int main()
{
    int retVal = 0;

    OpenSSL_add_all_algorithms();
    ERR_load_BIO_strings();
    ERR_load_crypto_strings();

    EVP_PKEY * pkey = EVP_PKEY_new();
    EC_KEY * ecc = EC_KEY_new_by_curve_name (OBJ_txt2nid (ECCTYPE));

    retVal = generate_keys (ecc, pkey);

    EVP_PKEY_free (pkey);
    EC_KEY_free (ecc);

    return retVal;
}
----

2. Secondly, I'd like to be able to work with these two generated keys... I
mean, encrypt, decrypt... but don't reach to understand how to use them and
which functions should be invoked ?

And also, I'd like to get the public/private keys... Should use
EC_KEY_get0_private_key() & EC_KEY_get0_public_key() functions ?

Any help will be also highly appreciated.

Matt L.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mta.openssl.org/pipermail/openssl-users/attachments/20151124/96d0acf7/attachment.html>


More information about the openssl-users mailing list