Reputation: 73
So far I can do the following. But I have no idea if it is working because I can't see the keys. I am planning on extracting them into a char array and storing them In a struct. So I need the full encoded byte array for the private and public keys.
I think I have it working partially for RSA. But I can't figure out how to use ED22519.
static bool GenerateEncryptionKeys(ofstream *file)
{
EVP_PKEY_CTX *ctx;
EVP_PKEY *pkey = NULL;
ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
if (ctx)
{
if (EVP_PKEY_keygen_init(ctx) > 0)
{
if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 2048) > 0)
{
if (EVP_PKEY_keygen(ctx, &pkey) > 0)
{
print("All good");
}
}
}
}
return.
Upvotes: 1
Views: 1011
Reputation: 73
Nevermind. I solved it.
static bool GenerateEncryptionKeys()
{
EVP_PKEY *my_pkey = nullptr;
EVP_PKEY_CTX *my_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_ED25519, nullptr);
EVP_PKEY_keygen_init(my_ctx);
EVP_PKEY_keygen(my_ctx, &my_pkey);
std::size_t pub_key_allocated_length;
EVP_PKEY_get_raw_public_key(my_pkey, NULL, &pub_key_allocated_length);
unsigned char *public_key = new unsigned char[pub_key_allocated_length];
EVP_PKEY_get_raw_public_key(my_pkey, public_key, &pub_key_allocated_length);
std::size_t pkey_allocated_length;
EVP_PKEY_get_raw_private_key(my_pkey, NULL, &pkey_allocated_length);
unsigned char *private_key = new unsigned char[pkey_allocated_length];
EVP_PKEY_get_raw_private_key(my_pkey, private_key, &pkey_allocated_length);
WriteEncryptionKeys(convertToString((const char *)private_key, pkey_allocated_length), convertToString((const char *)public_key, pub_key_allocated_length));
return true;
}
Upvotes: 1